Master Essential Git Commands: stash, reset‑soft, cherry‑pick, revert & reflog
This article walks developers through five practical Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purpose, common scenarios, exact usage syntax, handling conflicts, 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 basics of commit, pull, and push. This guide shares practical commands that boost efficiency and solve common problems.
stash
Official documentation: https://git-scm.com/docs/git-stash Git tutorial:
https://www.bookstack.cn/read/git-tutorial/docs-commands-git-stash.mdDescription
Official explanation: use git stash to record the current state of the working directory and index while returning to a clean working tree.
The stash command saves uncommitted changes so the working directory becomes clean.
Use Cases
When you need to switch branches urgently (e.g., to fix a production bug) but have uncommitted changes, git stash lets you store those changes without committing them.
Command Usage
git stashTo restore later:
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
# Clear all stash entries
git stash clear
# Apply the most recent stash
git stash apply
# Apply and drop the most recent 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:
git stash apply stash@{1}reset --soft
Official documentation: https://git-scm.com/docs/git-reset Git tutorial:
https://www.bookstack.cn/read/git-tutorial/docs-commands-git-reset.mdDescription
Resets the HEAD to a previous commit without touching the index or working tree, keeping the changes staged.
It rolls back a commit while preserving its changes in the staging area.
Use Cases
Useful when you accidentally commit something you didn’t intend to, or when you need to split a large commit into smaller, logical pieces.
Command Usage
# Undo the most recent commit but keep changes staged
git reset --soft HEAD^For already‑pushed commits, you can force‑push after resetting:
git push -fcherry-pick
Official documentation: https://git-scm.com/docs/git-cherry-pick Git tutorial:
http://www.ruanyifeng.com/blog/2020/04/git-cherry-pick.htmlDescription
Applies the changes introduced by existing commits onto the current branch, creating new commits.
It copies one or more existing commits to another branch.
Use Cases
When a specific feature needs to be released urgently, or when a branch has become polluted and you want to cherry‑pick clean commits onto a fresh branch.
Command Usage
Single commit
git cherry-pick <commitHash>Multiple commits
git cherry-pick commit1 commit2Or a range:
git cherry-pick commit1^..commit2Handling Conflicts
If conflicts occur, resolve them, stage the changes, then continue: git cherry-pick --continue To abort the operation: git cherry-pick --abort To quit while keeping already‑applied commits:
git cherry-pick --quitrevert
Official documentation:
https://git-scm.com/docs/git-revertDescription
Creates a new commit that undoes the changes introduced by an existing commit (or commits), requiring a clean working tree.
It is useful for quickly rolling back a problematic change without rewriting history.
Use Cases
When a released feature causes bugs and you need to revert it without affecting other teammates' work.
Command Usage
Reverting a regular commit
git revert 21dcd937fe555f58841b17466a99118deb489212Reverting a merge commit
git revert -m 1 <commitHash>After reverting, further merges of the same changes may be ignored unless you revert the revert.
reflog
Official documentation:
https://git-scm.com/docs/git-reflogDescription
Manages the reference log, recording updates to the tip of branches and other references.
It acts as a powerful “undo” tool, letting you recover lost commits after mistaken resets.
Use Cases
If you accidentally reset too far and lost commits, git reflog shows the previous HEAD positions so you can restore them.
Command Usage
git reflogIdentify the desired commit hash from the log and reset back to it:
git reset --hard <commitHash>Setting Git Short Commands
Two ways to create aliases for faster typing: git config --global alias.ps push Or edit ~/.gitconfig and add:
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickThen use, for example:
git cp <commitHash>Summary
The article highlights five practical Git commands and how to set short aliases: stash: store temporary changes. reset --soft: roll back a commit while keeping changes staged. cherry-pick: copy commits. revert: undo a commit’s changes. reflog: view the history of reference updates.
Understanding these commands and customizing aliases can greatly improve daily development efficiency.
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
