Fundamentals 14 min read

Master Git: Stash, Reset‑Soft, Cherry‑Pick, Revert, and Reflog Explained with Real‑World Scenarios

This article presents practical Git commands—including stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purpose, typical use‑cases, step‑by‑step examples, and how to integrate them into everyday development workflows to avoid common pitfalls and improve efficiency.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Git: Stash, Reset‑Soft, Cherry‑Pick, Revert, and Reflog Explained with Real‑World Scenarios

Introduction

Git is an essential version‑control tool for developers, yet many only use the basic save‑pull‑push workflow. This guide shares five advanced commands that solve common commit‑management problems and boost productivity.

stash

Description : git stash saves the current working‑directory and index state, leaving a clean directory while preserving uncommitted changes.

Typical scenario : While working on a feature branch, an urgent bug fix is required on master. Switching branches would normally be blocked by uncommitted changes.

Usage : git stash After fixing the bug, restore the saved changes: git stash apply Additional useful commands:

# Save with a message
git stash save "my note"
# List all stashes
git stash list
# Remove all stashes
git stash clear
# Apply the most recent stash and drop it
git stash pop
# Drop a specific stash
git stash drop
# Apply a specific stash
git stash apply stash@{1}

When multiple stashes exist, list them with git stash list and apply a chosen one, e.g. git stash apply stash@{1}.

reset --soft

Description : git reset --soft moves HEAD to a previous commit while keeping all changes staged, allowing you to amend or recombine commits without losing work.

Typical scenarios :

Accidentally committed changes that should not have been recorded.

Team policies require fine‑grained commits; a large accidental commit needs to be split.

Usage :

# Undo the most recent commit but keep changes staged
git reset --soft HEAD^

If the commit has already been pushed, you must force‑push the corrected history: git push -f When specifying a commit hash, git reset --soft <hash> restores all changes from that commit up to the current HEAD into the index.

cherry‑pick

Description : git cherry-pick copies one or more existing commits onto the current branch, creating new commits with the same changes.

Typical scenarios :

A completed feature needs to be released urgently while other work is still in progress.

A polluted development branch must be cleaned by extracting only the desired commits.

Usage :

Single commit

git cherry-pick <commitHash>

Multiple commits

git cherry-pick commit1 commit2

Range of commits

git cherry-pick commit1^..commit2

If conflicts occur, resolve them, 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 --quit

revert

Description : git revert creates a new commit that undoes the changes introduced by a specified commit (or merge commit), preserving history.

Typical scenario : An urgent production bug requires rolling back a feature without losing other teammates’ work.

Usage for a regular commit :

git revert 21dcd937fe555f58841b17466a99118deb489212

For a merge commit, specify the parent to keep (usually -m 1 to retain the mainline): git revert -m 1 <mergeCommitHash> Reverting a merge may prevent the same changes from being merged again; to re‑apply them, revert the revert commit.

reflog

Description : git reflog records updates to the tip of branches, allowing recovery of commits lost by mistaken resets or other destructive actions.

Typical scenario : After an accidental git reset --hard that removed recent commits, git reflog can locate the lost commit hashes and restore them.

Usage :

# Show the reference log
git reflog
# Reset back to a previous state using the hash from reflog
git reset --hard <commitHash>

Setting short Git aliases

Define convenient shortcuts in ~/.gitconfig:

[alias]
  co = checkout
  ps = push
  pl = pull
  mer = merge --no-ff
  cp = cherry-pick
  # Example of a one‑line alias
  ps = push

Alternatively, create a single alias via the command line:

git config --global alias.ps push

Conclusion

The five commands— stash, reset --soft, cherry-pick, revert, and reflog —along with custom aliases, give developers powerful tools to manage code history, recover from mistakes, and keep branches clean. Understanding their behavior and applying them appropriately maximizes workflow efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitVersion Controlresetcherry-pickstashrevertreflog
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.