Master Essential Git Commands: stash, reset‑soft, cherry-pick, revert & reflog
This guide walks developers through five practical Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purpose, typical scenarios, exact syntax, and how to integrate them into daily workflows, plus tips for creating short Git aliases.
Introduction
Git is a fundamental tool for developers, yet many only use the basic save‑pull‑push cycle. This article presents five practical Git commands that solve common workflow problems and boost productivity.
git stash
Description
Stores uncommitted changes and restores a clean working directory, allowing you to switch branches without committing incomplete work.
Typical scenario
You are developing a feature when an urgent bug must be fixed on master. Instead of committing a placeholder, you can stash your work.
Commands
git stashApply the stash after fixing the bug: git stash apply Additional useful stash commands: git stash save "message" – stash with a note git stash list – list all stashes git stash clear – delete all stashes git stash pop – apply and drop the latest stash git stash drop – delete a specific stash
When multiple stashes exist, list them with git stash list and apply a specific one, e.g. git stash apply stash@{1}.
git reset --soft
Description
Moves the HEAD pointer to a previous commit while keeping all changes staged (in the index). It acts as a “soft” undo, preserving modifications for a new commit.
Typical scenarios
Accidentally committed unwanted changes; you want to redo the commit without creating extra history.
Team policies require granular commits; you need to split a large commit into smaller, logical ones.
Command
# Undo the most recent commit but keep changes staged
git reset --soft HEAD^If the commit has already been pushed, you must force‑push after the reset: git push -f. When specifying a commit hash, all changes from that commit up to the current HEAD are staged.
git cherry-pick
Description
Copies one or more existing commits and creates new commits on the current branch, preserving the original changes.
Typical scenarios
Need to apply a specific feature or bug‑fix commit to another branch without merging the whole branch.
Cleaning a polluted development branch by cherry‑picking only the desired commits onto a fresh branch.
Single commit
Identify the commit hash on the feature branch, switch to master, then run:
git cherry-pick <commitHash>Multiple commits
git cherry-pick commit1 commit2Or a range:
git cherry-pick commit1^..commit2Handling conflicts
If a conflict occurs, resolve it, stage the changes, and continue with: git cherry-pick --continue To abort the operation: git cherry-pick --abort To quit while keeping already applied commits:
git cherry-pick --quitgit revert
Description
Creates a new commit that undoes the changes introduced by a previous commit (or merge). The working tree must be clean.
Typical scenario
A recently deployed feature causes a critical bug. Instead of resetting the branch (which would discard other teammates' work), you can revert the offending commit.
Reverting a normal commit
git revert 21dcd937fe555f58841b17466a99118deb489212Reverting a merge commit
Specify the parent number (usually 1) to indicate which side of the merge should be kept:
git revert -m 1 <mergeCommitHash>git reflog
Description
Records updates to the tip of branches and HEAD, allowing you to recover lost commits after accidental resets.
Typical scenario
After a mistaken git reset --hard that removed a colleague’s commit, you can locate the lost commit hash via git reflog and reset back to it.
git reflog
# find the desired entry, then
git reset --hard <commitHash>Setting short Git aliases
Method 1 – Global config
git config --global alias.ps pushMethod 2 – Edit .gitconfig
vim ~/.gitconfigAdd an [alias] section, e.g.:
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickUse the alias like git cp <commitHash> to run git cherry-pick quickly.
Conclusion
The article covered five practical Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—plus two ways to create short aliases, providing developers with concrete tools to handle everyday version‑control challenges efficiently.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
