Master Essential Git Commands: stash, reset --soft, cherry-pick, revert & reflog
Learn how to efficiently manage code with Git by mastering five practical commands—stash, reset --soft, cherry-pick, revert, and reflog—including their descriptions, real-world scenarios, step-by-step usage, conflict resolution, and shortcuts for faster development workflows.
Preface
Using Git as a version control system is essential for developers, yet many only know basic save, pull, and push and struggle with commit management.
This article shares practical commands that can greatly improve efficiency and solve common scenarios, with hands‑on examples.
stash
Description
Official: use git stash to record the current state of the working directory and index while returning a clean working directory.
Stash saves uncommitted changes so the working directory becomes clean.
Use case
When developing a feature and a critical bug must be fixed on master, you need a clean state to switch branches without committing unfinished work.
Committing a placeholder creates a noisy history; stash avoids that.
Command
Simply run: git stash After fixing the bug, restore the changes: git stash apply Related commands:
# Save uncommitted changes
git stash
# Save with a message
git stash save "message"
# List stash entries
git stash list
# Clear all stash entries
git stash clear
# Apply most recent stash
git stash apply
# Apply and drop
git stash pop
# Drop a specific stash
git stash dropVSCode integration
VSCode UI for stash operations:
reset --soft
Description
Reset moves the HEAD to a previous commit without touching the index or working tree, leaving changes staged.
It rolls back commits while keeping modifications in the staging area.
Use case
When an accidental commit was made or commits need to be split for clearer history.
Command
# Restore the most recent commit
git reset --soft HEAD^For already pushed commits, you must force‑push after resetting.
Example of resetting to a specific commit restores its changes to the index.
cherry-pick
Description
Applies the changes introduced by existing commits onto the current branch, creating new commits.
Use case
Copy specific commits to another branch, e.g., when a feature needs to be released early or to clean a polluted branch.
Command
Single commit
Identify the commit hash, switch to target branch, then:
git cherry-pick <commitHash>Multiple commits
git cherry-pick commit1 commit2Or a range:
git cherry-pick commit1^..commit2Conflict handling
If conflicts occur, resolve them, then continue: git cherry-pick --continue To abort: git cherry-pick --abort To quit while keeping successful picks:
git cherry-pick --quitrevert
Description
Creates a new commit that undoes the changes introduced by an existing commit.
Use case
When a released feature causes problems and must be rolled back without affecting other work.
Command
Reverting a regular commit
git revert <commitHash>Reverting a merge commit
Specify the parent to keep with -m: git revert -m 1 <commitHash> After reverting a merge, subsequent merges of the same changes may be ignored; you can revert the revert to re‑apply.
reflog
Description
Reflog records updates to the tip of branches and HEAD, allowing recovery of lost commits.
Use case
When an accidental hard reset removes recent commits, reflog can locate the lost commit hash.
Command
View history: git reflog Then reset to the desired entry:
git reset --hard <commitHash>Setting short Git commands
Define aliases to speed up workflow: git config --global alias.ps push Or edit ~/.gitconfig:
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickUse the alias:
# Equivalent to git cherry-pick <commitHash>
git cp <commitHash>Summary
This article covered five useful Git commands—stash, reset --soft, cherry-pick, revert, and reflog—and how to set short aliases for faster development.
stash: temporarily store changes.
reset --soft: roll back commits while keeping changes staged.
cherry-pick: copy commits.
revert: undo commit changes.
reflog: track commit history.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
