Fundamentals 19 min read

Practical Git Commands: stash, reset --soft, cherry-pick, revert, and reflog with Usage Scenarios

This article presents practical Git commands—including stash, reset --soft, cherry-pick, revert, and reflog—explaining their descriptions, typical use cases, and step‑by‑step examples, along with tips for configuring short aliases, helping developers efficiently manage code changes and recover from mistakes.

Top Architect
Top Architect
Top Architect
Practical Git Commands: stash, reset --soft, cherry-pick, revert, and reflog with Usage Scenarios

Using Git for code version management is a must‑have skill for developers, yet many only know the basic save, pull, and push operations. This article shares five useful Git commands, their application scenarios, and hands‑on examples to help readers master them quickly.

stash

Official documentation [1] Git tutorial [2]

Description

Official explanation: use git stash when you want to record the current state of the working directory and index but need a clean working directory. The command saves local modifications and restores the working directory to match the HEAD commit.

The stash command stores uncommitted code, making the working directory clean.

Application scenario

When you are developing a feature on a branch and a critical bug appears on master , you need to switch branches but have uncommitted changes. Instead of committing a meaningless "temporary code" message, you can stash the changes, switch branches, fix the bug, then apply the stash back.

Command usage

Store current uncommitted code:

git stash

Apply the most recent stash after fixing the bug:

git stash apply

Related commands

# Save current uncommitted code
git stash

# Save with a message
git stash save "
message
"

# List all stash records
git stash list

# Delete all stash records
git stash clear

# Apply the most recent stash
git stash apply

# Apply and drop the stash
git stash pop

# Drop the most recent stash
git stash drop

If multiple stashes exist, list them with git stash list and apply a specific one:

$ git stash list
stash@{0}: WIP on ...
stash@{1}: WIP on ...

Apply the second stash:

$ git stash apply stash@{1}

reset --soft

Official documentation [3] Git tutorial [4]

Description

It resets the HEAD to a previous commit without touching the index or working tree, keeping the changes staged.

Use git reset --soft to undo a commit while preserving its modifications in the staging area.

Application scenario

1) You accidentally committed code that should not be in the history; instead of adding another commit, you can reset softly and amend.

2) Teams require fine‑grained commits; if you mistakenly bundled unrelated changes into one commit, reset --soft lets you split them.

Command usage

Recover the most recent commit:

# Recover the most recent commit
git reset --soft HEAD^

After fixing, you can push forcefully if the commit was already pushed:

git push -f

cherry-pick

Official documentation [5] Git cherry-pick tutorial [6]

Description

Given one or more existing commits, apply each commit’s changes as a new commit on the current branch. The working tree must be clean.

Application scenario

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

2) A polluted development branch must be cleaned; cherry‑pick desired commits onto a fresh branch.

Command usage

Copy a single commit

Identify the commit hash on the feature branch and apply it to master :

git cherry-pick

Copy multiple commits

Specify each commit:

git cherry-pick commit1 commit2

Or a range (inclusive):

git cherry-pick commit1^..commit2

Cherry‑pick conflict handling

If a conflict occurs, Git stops. Resolve the conflict, stage the changes, then continue:

git cherry-pick --continue

To abort the whole process:

git cherry-pick --abort

To quit while keeping already applied commits:

git cherry-pick --quit

revert

Official documentation [7]

Description

Reverts one or more existing commits by creating new commits that reverse the changes. The working tree must be clean.

Application scenario

When a released feature causes a critical issue, revert can safely undo the problematic changes without affecting later commits.

Command usage

Revert a normal commit

git revert 21dcd937fe555f58841b17466a99118deb489212

Revert a merge commit

Specify the parent to keep (usually -m 1 ):

git revert -m 1

After reverting a merge, re‑merging the same branch later may be ignored because Git sees the original commits as already applied; you can revert the revert to bring the changes back.

reflog

Official documentation [8]

Description

This command records updates to the tip of branches and other references.

When a hard reset removes commits accidentally, git reflog lets you find the lost commit hashes and recover them.

Command usage

git reflog

Identify the lost commit hash from the reflog output, then reset back:

git reset --hard

Setting Git short commands (aliases)

Method 1

git config --global alias.ps push

Method 2

Edit the global config file:

vim ~/.gitconfig

Add aliases:

[alias]
    co = checkout
    ps = push
    pl = pull
    mer = merge --no-ff
    cp = cherry-pick

Use the short alias:

# Equivalent to git cherry-pick
git cp

Summary

stash : store temporary code changes.

reset --soft : soft reset, keep modifications.

cherry-pick : copy commits.

revert : undo commit changes.

reflog : view history of reference updates.

The article also notes that some listed scenarios are simplified for illustration; the key is to understand each command’s purpose and apply it flexibly.

workflowsoftware developmentgitcommand-lineversion controlreflog
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

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