Fundamentals 14 min read

Master Git Stash, Reset, Cherry-pick, Revert & Reflog: Essential Commands

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 handling, and how to create short aliases for faster workflow.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Master Git Stash, Reset, Cherry-pick, Revert & Reflog: Essential Commands

stash

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

Typical scenario: you are working on a feature branch and a critical bug must be fixed on master. You cannot switch branches because you have uncommitted changes, so you stash them, fix the bug, then apply the stash back. git stash After fixing the bug, restore the saved changes: git stash apply Related commands:

# Save uncommitted changes
git stash
# Save with a message
git stash save "message"
# 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 stash
git stash pop
# Drop a specific stash
git stash drop

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

reset --soft

git reset --soft

moves the HEAD to a previous commit while leaving the changes from the undone commits staged (in the index). It is useful when you want to undo a commit but keep its modifications for editing.

Scenario 1: you accidentally committed changes that should not be in the history. git reset --soft HEAD^ rewinds one commit, leaving the changes staged for a new, clean commit.

Scenario 2: a team requires fine‑grained commits; a large accidental commit can be split after a soft reset. git reset --soft HEAD^ If the unwanted commit has already been pushed, you can force‑push the corrected history with git push -f. Specifying a commit hash resets to that commit and stages all changes introduced after it.

cherry-pick

git cherry-pick <commit>

copies the changes introduced by an existing commit and creates a new commit on the current branch. The working tree must be clean.

Use cases: extracting a single feature for an urgent release, or moving clean commits from a polluted development branch to a new branch.

# Copy a single commit
git cherry-pick abc123
# Copy multiple commits
git cherry-pick commit1 commit2
# Copy a range of commits (inclusive)
git cherry-pick commit1^..commit2

When conflicts occur during a multi‑commit cherry‑pick, Git stops. Resolve the conflicts, stage the changes, then continue with git cherry-pick --continue. To abort the operation, use git cherry-pick --abort; to quit while keeping already applied commits, use git cherry-pick --quit.

revert

git revert <commit>

creates a new commit that undoes the changes introduced by the specified commit, leaving the original commit in history. The working tree must be clean.

Typical scenario: a production bug requires an immediate rollback without rewriting shared history. Reverting generates a new commit that reverses the problematic changes.

git revert 21dcd937fe555f58841b17466a99118deb489212

Reverting a merge commit requires specifying the parent to keep (usually -m 1) because Git needs to know which side of the merge is the mainline. git revert -m 1 <merge‑commit‑hash> After a revert, the original commit remains, but its changes are undone. If you later need to re‑apply the reverted changes, you can revert the revert commit.

reflog

git reflog

records updates to the tip of branches and other references, acting as a powerful “undo” log. It helps recover lost commits after mistaken resets.

Example workflow: after an accidental git reset --hard that removed a teammate’s commit, run git reflog to find the lost commit hash and reset back to it.

git reflog
# Identify the desired entry, then
git reset --hard <commit‑hash>

Setting Git Short Commands (Aliases)

Define shortcuts to speed up command entry. Two methods:

# Method 1 – one‑liner
git config --global alias.ps push

Method 2 – edit the global config file: vim ~/.gitconfig Add an [alias] section, e.g.:

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

Use the alias like any other Git command:

# Equivalent to "git cherry-pick <commitHash>"
git cp <commitHash>

Summary

The article presents five practical Git commands— stash, reset --soft, cherry-pick, revert, and reflog —and shows how to create short aliases for faster workflow.

resetcherry-pickstashrevertreflog
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

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.