Fundamentals 14 min read

Master Essential Git Commands: stash, reset --soft, cherry-pick, revert, reflog & shortcuts

This guide walks developers through five practical Git commands—stash, reset --soft, cherry-pick, revert, and reflog—explaining their purpose, typical scenarios, step‑by‑step usage, conflict resolution, and how to create short aliases for faster workflow.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Essential Git Commands: stash, reset --soft, cherry-pick, revert, reflog & shortcuts

stash

Use git stash to record the current state of the working directory and index, then revert to a clean tree.

Typical scenario

You are working on a feature branch and an urgent bug must be fixed on master. Uncommitted changes prevent you from checking out master. Instead of creating a meaningless temporary commit, you can stash the changes.

Basic commands

git stash                 # Save all uncommitted changes
git stash save "<em>message</em>"   # Save with a descriptive message
git stash list            # Show all stash entries
git stash apply [stash@{n}] # Re‑apply a stash (default: latest)
git stash pop             # Apply and immediately drop the stash
git stash drop [stash@{n}] # Delete a specific stash
git stash clear           # Remove all stash entries

Working with multiple stashes

List entries with git stash list and apply a specific one, e.g. git stash apply stash@{1}.

reset --soft

Reset HEAD to a previous commit without touching the index or working tree, leaving all changes staged.

When to use

Accidentally committed unwanted changes and want to undo the commit while keeping the modifications.

Need to split a large commit into smaller, logically separated commits.

Command

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

If the commit has already been pushed, force‑push the corrected history: git push -f When a specific commit hash is supplied, --soft moves HEAD to that commit and stages all changes introduced after it.

cherry-pick

Apply the changes introduced by one or more existing commits onto the current branch, creating new commits for each.

Typical scenarios

Release a single feature while other work remains unfinished.

Clean a polluted development branch by copying only the desired commits onto a fresh branch.

Single commit

git cherry-pick <commitHash>

Multiple commits

# List of individual commits
git cherry-pick <commit1> <commit2>
# Range of consecutive commits (inclusive)
git cherry-pick <commitA>^..<commitB>

Conflict handling

If a conflict occurs, resolve the files, stage the changes, then continue: git cherry-pick --continue To abort the whole operation and return to the pre‑cherry‑pick state: git cherry-pick --abort To stop the operation while keeping already applied commits:

git cherry-pick --quit

revert

Create a new commit that undoes the changes introduced by an existing commit (or merge).

When to use

Use git revert to roll back a faulty change without rewriting history, which is safe for already‑published branches.

Reverting a regular commit

git revert <commitHash>

This opens the editor for the revert commit message; save and exit to create the revert commit.

Reverting a merge commit

Specify the mainline parent with -m (usually 1) to indicate which side of the merge should be kept: git revert -m 1 <mergeCommitHash> After reverting a merge, subsequent merges of the same branch may be ignored because Git records that the changes have already been applied.

reflog

Record updates to the tip of branches and other references, providing a powerful undo log.

Typical scenario

If you reset too far and lose a commit, you can recover it via the reflog.

Commands

# Show the reference log
git reflog
# Reset to a specific entry from the reflog
git reset --hard <commitHashFromReflog>

Identify the desired entry in the reflog output, copy its hash, and reset to it.

Setting Git short aliases

Define short aliases to reduce typing for frequently used commands.

Method 1 – command line

git config --global alias.ps push
git config --global alias.co checkout
git config --global alias.pl pull
git config --global alias.mer "merge --no-ff"
git config --global alias.cp cherry-pick

Method 2 – edit ~/.gitconfig

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

After defining the aliases, you can run git cp <commitHash> instead of the full git cherry-pick <commitHash> command.

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.