Practical Git Commands: stash, reset --soft, cherry‑pick, revert, reflog and Short Aliases
This article introduces five essential Git commands—stash, reset --soft, cherry‑pick, revert, reflog—explains their typical scenarios, provides step‑by‑step usage examples with code snippets, and shows how to configure short aliases to boost development efficiency.
Introduction
Git is a must‑know tool for developers, yet many only master the basics of pull, push, and commit. This guide shares practical commands that can dramatically improve workflow and solve common problems.
stash
Description
Stash saves uncommitted changes and restores a clean working directory.
Use Cases
When you need to switch branches urgently (e.g., to fix a production bug) but have unfinished work, stash lets you store the changes without committing them.
Command Usage
git stashTo apply the saved changes later:
git stash applyRelated Commands
# Save current uncommitted changes
git stash
# Save with a message
git stash save "
"
# 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 dropreset --soft
Description
Reset --soft moves the HEAD to a previous commit while keeping the changes staged, effectively acting as a “soft undo”.
Use Cases
Useful when you accidentally commit unwanted changes and want to amend them without creating extra history.
Command Usage
git reset --soft HEAD^After resetting, you can modify the changes and recommit. For already‑pushed commits, you need to force‑push:
git push -fcherry‑pick
Description
Cherry‑pick copies one or more existing commits and creates new commits on the current branch.
Use Cases
When a specific feature or bug‑fix needs to be applied to another branch without merging the whole branch.
Command Usage
Copy a single commit:
git cherry-pickCopy multiple commits:
git cherry-pick commit1 commit2Copy a range of commits:
git cherry-pick commit1^..commit2Handling Conflicts
If conflicts occur, resolve them, stage the changes, and continue:
git cherry-pick --continueTo abort or quit the operation:
git cherry-pick --abort git cherry-pick --quitrevert
Description
Revert creates a new commit that undoes the changes introduced by a previous commit.
Use Cases
When a released feature causes issues and you need to roll back its changes without affecting other commits.
Command Usage
git revertTo revert a merge commit, specify the parent:
git revert -m 1reflog
Description
Reflog records all movements of HEAD, allowing you to recover lost commits after mistaken resets.
Use Cases
If you accidentally reset too far and lost a commit, reflog lets you find the missing commit hash and restore it.
Command Usage
git reflogFind the desired commit hash from the reflog output and reset back to it:
git reset --hardSetting Short Git Aliases
Method 1
git config --global alias.ps pushMethod 2
Edit the global config file (~/.gitconfig) and add aliases:
[alias]
co = checkout
ps = push
pl = pull
mer = merge --no-ff
cp = cherry-pickNow you can run git cp <commitHash> as a shortcut for git cherry-pick .
Conclusion
The article covered five practical Git commands—stash, reset --soft, cherry‑pick, revert, reflog—and demonstrated how to create short aliases to speed up development.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.