Fundamentals 16 min read

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, and reflog—explaining their purposes, typical scenarios, and step‑by‑step usage, and also shows how to create convenient short aliases for frequent Git operations.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Practical Git Commands: stash, reset --soft, cherry-pick, revert, reflog and Short Aliases

Introduction

Using Git for source code version control is a must‑have skill for modern developers. Many engineers only know the basic save, pull, and push operations and struggle with more advanced commit management scenarios.

This article shares practical commands I have used in development, which can greatly improve efficiency and solve many tricky situations. The commands are presented with usage scenarios and hands‑on instructions so readers can master them immediately.

stash

Description

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

The stash command stores uncommitted code, allowing the working directory to become clean.

Use Cases

Why make the working directory clean? Imagine you are developing a new feature on a feature branch and a critical bug is reported that must be fixed immediately on master. You need to switch branches, but Git refuses because you have uncommitted changes.

Instead of committing a meaningless "temporary code" message, you can simply stash your changes.

Command Usage

To stash your changes:

git stash

When the urgent bug is fixed and you switch back to the feature branch, restore the code with:

git stash apply

Related Commands

# Save current uncommitted code
git stash

# Save with a message
git stash save "your 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 then delete 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 ...
stash@{2}: On ...

Apply the second stash:

$ git stash apply stash@{1}

The pop and drop commands work similarly.

VS Code Integration

In VS Code, the stash UI allows you to add a message (or just press Enter), view saved stashes, and apply or pop them via the Stashes menu.

reset --soft

Description

git reset --soft moves the HEAD to a specified commit without touching the index or working tree, keeping all changes staged ("to be committed"). It is often contrasted with git reset --hard , which discards changes.

Use Cases

1. You accidentally committed something that should not have been committed; you want to undo the commit without losing the changes. 2. Your team enforces fine‑grained, well‑described commits. If you mistakenly combine unrelated changes into a single commit, reset --soft lets you rewrite the history cleanly.

Command Usage

To undo the most recent commit while keeping changes staged:

# Restore the latest commit
git reset --soft HEAD^

This acts like a "regret pill", giving you a chance to modify and recommit.

If the commit has already been pushed, you must force‑push after resetting:

git push -f

When specifying a commit hash, reset --soft restores all changes from that commit up to the current HEAD into the index.

cherry-pick

Description

git cherry-pick applies the changes introduced by an existing commit (or a series of commits) onto the current branch, creating new commit(s) for each applied change. The working tree must be clean.

Use Cases

1. A completed feature needs to be released urgently while other work is still in progress. 2. A polluted development branch requires a clean branch; you cherry‑pick the good commits onto the new branch.

Command Usage

Single commit

Copy commit b from a feature branch to master:

git cherry-pick

Multiple commits

git cherry-pick commit1 commit2

Or a range of consecutive commits:

git cherry-pick commit1^..commit2

Cherry‑pick Conflicts

When cherry‑picking multiple commits, conflicts may arise. Resolve the conflicts, stage the changes, and continue with:

git cherry-pick --continue

To abort the whole 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 commits). The working tree must be clean.

Use Cases

When a recently deployed feature causes a critical issue, you can revert the offending commit without rewriting history, preserving other teammates' work.

Command Usage

Revert a normal commit:

git revert 21dcd937fe555f58841b17466a99118deb489212

Reverting a merge commit requires specifying the parent (usually -m 1 to keep the mainline):

git revert -m 1 <mergeCommitHash>

After reverting a merge, subsequent merges of the original branch may be ignored because Git sees the changes as already applied; you can revert the revert to re‑introduce the changes.

reflog

Description

git reflog records updates to the tip of branches and other references, acting as a powerful "undo" tool for recovering lost commits.

Use Cases

If you accidentally performed a hard reset to the wrong commit and lost others' work, you can view the reflog to find the lost commit hashes and reset back to them.

Command Usage

View the reflog:

git reflog

Identify the desired commit hash and reset to it:

git reset --hard <commitHash>

Setting Git Short Commands (Aliases)

Method 1

git config --global alias.ps push

Method 2

Edit the global config file:

vim ~/.gitconfig

Add alias definitions:

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

Usage

# Equivalent to "git cherry-pick
"
git cp

Summary

This article shares five practical Git commands— stash , reset --soft , cherry-pick , revert , and reflog —and demonstrates how to set short aliases to speed up daily development.

stash : store temporary code.

reset --soft : soft rollback, keeping changes staged.

cherry-pick : copy commits.

revert : undo a commit's changes.

reflog : record commit history.

The listed scenarios may not be perfect, but the goal is to understand each command's purpose and apply them flexibly.

Final Note (Please Follow)

If this article helped you, please like, watch, share, and bookmark—it’s the biggest motivation for me to keep writing.

gitversion controlresetcherry-pickstashrevertreflog
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.