Master Git Stash, Reset, Cherry-Pick, Revert & Reflog: Practical Commands
This guide walks developers through five essential Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purposes, real‑world scenarios, and step‑by‑step usage, plus tips for creating handy Git aliases to boost workflow efficiency.
Using Git as a version‑control system is a core skill for developers, yet many only know basic save, pull, and push operations. This article shares practical Git commands that greatly improve efficiency and solve common scenarios.
stash
Description
The git stash command records the current state of the working directory and index, then restores a clean working tree. It temporarily saves uncommitted changes.
Application Scenario
When a feature branch is in the middle of development and an urgent bug must be fixed on master, git stash lets you switch branches without committing incomplete work.
Command Usage
git stashAfter fixing the bug, restore the saved changes:
git stash applyRelated Commands
# Save current uncommitted changes
git stash
# Save with a message
git stash save "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 delete the most recent stash
git stash pop
# Delete a specific stash
git stash dropWhen multiple stashes exist, list them and apply a specific one:
git stash list
git stash apply stash@{1}reset --soft
Description
git reset --softmoves the HEAD to a previous commit while keeping all changes staged, effectively acting as a “soft undo” that preserves modifications.
Application Scenario
Accidentally committed unfinished work or need to amend a commit before pushing. --soft lets you modify the commit without losing changes.
Command Usage
git reset --soft HEAD^To reset to a specific commit:
git reset --soft 1a900ac29eba73ce817bf959f82ffcb0bfa38f75If the commit has already been pushed, force‑push the corrected history:
git push -fcherry-pick
Description
git cherry-pickapplies the changes introduced by existing commits onto the current branch, creating new commits for each.
Application Scenario
When a specific feature or fix needs to be transferred to another branch without merging the entire branch history.
Command Usage
Copy a single commit: git cherry-pick <commitHash> Copy multiple commits: git cherry-pick commit1 commit2 Copy a range of commits: git cherry-pick commit1^..commit2 If conflicts occur, resolve them, 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
Description
git revertcreates a new commit that undoes the changes introduced by a given commit (or range), preserving a clean history.
Application Scenario
Urgently roll back a problematic feature without affecting other teammates' work.
Command Usage
Revert a regular commit:
git revert 21dcd937fe555f58841b17466a99118deb489212Revert a merge commit (specify the mainline parent):
git revert -m 1 <mergeCommitHash>reflog
Description
git reflogrecords updates to references, allowing you to recover lost commits after operations like a hard reset.
Application Scenario
After an accidental reset --hard that removed a colleague’s commit, use reflog to find the missing commit hash and restore it.
Command Usage
git reflogIdentify the desired entry, then reset back to it:
git reset --hard <commitHash>Setting Git Short Commands (Aliases)
Method 1
git config --global alias.ps pushMethod 2
Edit the global config file: vim ~/.gitconfig Add alias definitions:
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickSummary
stash: temporarily store uncommitted changes. reset --soft: roll back commits while keeping changes staged. cherry-pick: copy specific commits to another branch. revert: create a new commit that undoes previous changes. reflog: view reference history to recover lost commits.
The examples illustrate real‑world scenarios and command sequences to help developers use these Git features effectively.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
