Master Git Stash, Reset, Cherry‑pick, Revert, and Reflog: Practical Commands for Developers
This article walks developers through five essential Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purpose, common scenarios, step‑by‑step usage, conflict handling, and how to create short aliases for faster workflow.
Preface
Using Git for version control is a core skill for developers, yet many only know the basic save‑pull‑push workflow and struggle with commit management. This guide shares practical commands that boost efficiency and solve common problems.
stash
Description
Official definition: use git stash to record the current state of the working directory and index while returning to a clean working directory.
The stash command saves uncommitted changes so the working directory becomes clean.
Use Cases
When you are working on a feature branch and need to switch to master to fix an urgent bug, Git will refuse the switch if there are uncommitted changes. Instead of committing a meaningless “temporary code” message, you can stash the changes.
Command Usage
git stashTo restore the saved changes after fixing the bug:
git stash applyRelated Commands
# Save current uncommitted changes
git stash
# Save with a message
git stash save "your message"
# List all stash entries
git stash list
# Delete all stash entries
git stash clear
# Apply the most recent stash
git stash apply
# Apply and delete the stash
git stash pop
# Drop the most recent stash
git stash dropWhen multiple stashes exist, list them with git stash list and apply a specific one, e.g.:
git stash apply stash@{1}VS Code Integration
VS Code shows saved stashes in the STASHES menu; you can apply or pop a stash using the arrow next to each entry.
reset --soft
Description
Resets the HEAD to a previous commit without touching the index or working tree, leaving the changes staged for a new commit.
It rolls back a commit while keeping its modifications in the staging area.
Use Cases
1️⃣ You accidentally committed code that should not have been included; reset --soft lets you undo the commit while preserving the changes for a proper commit. 2️⃣ In teams that require fine‑grained commits, a single accidental commit can be split into separate logical commits using this command.
Command Usage
git reset --soft HEAD^For already pushed commits, you may need to force‑push after resetting: git push -f When specifying a commit hash, reset --soft restores all changes from that commit up to the current HEAD.
cherry‑pick
Description
Applies the changes introduced by existing commit(s) onto the current branch, creating new commit(s). The working tree must be clean.
Use Cases
• A completed feature needs to be released immediately while other work is still in progress. • A polluted development branch must be cleaned; you create a fresh branch and cherry‑pick the desired commits.
Command Usage
Single commit
git cherry-pick <commitHash>Multiple commits
# List of commits
git cherry-pick commit1 commit2
# Range of consecutive commits
git cherry-pick commit1^..commit2Conflict Handling
If a conflict occurs, resolve it, stage the changes, and continue: git cherry-pick --continue To abort the operation: git cherry-pick --abort To quit while keeping already applied commits:
git cherry-pick --quitrevert
Description
Creates a new commit that undoes the changes introduced by an existing commit (or commits). The working tree must be clean.
Use Cases
When a released feature causes a critical bug, git revert safely creates a rollback commit without rewriting history.
Command Usage
Reverting a regular commit
git revert 21dcd937fe555f58841b17466a99118deb489212Reverting a merge commit
Specify the parent to keep (usually -m 1 for the mainline): git revert -m 1 <mergeCommitHash> After reverting a merge, subsequent merges of the same changes may be ignored because Git sees the commit hash as already applied; you can revert the revert to re‑apply the changes.
reflog
Description
Records updates to the tip of branches and other references, allowing you to recover lost commits.
Use Cases
If you mistakenly performed a hard reset and lost recent commits, git reflog lets you locate the lost commit hashes and restore them.
Command Usage
git reflogFind the desired entry, then reset back to it:
git reset --hard <commitHashFromReflog>Setting Short Git Commands
Define aliases to speed up typing.
Method 1 – Global config
git config --global alias.ps pushMethod 2 – Edit ~/.gitconfig
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickUse the alias like any other Git command, e.g. git cp <commitHash>.
Summary
The article presents five practical Git commands— stash, reset --soft, cherry-pick, revert, and reflog —along with ways to create short aliases, helping developers manage code changes efficiently and recover from mistakes.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
