Fundamentals 14 min read

Master Git Stash, Reset, Cherry-pick, Revert & Reflog for Clean Code Management

This guide walks developers through practical Git commands—including 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.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Master Git Stash, Reset, Cherry-pick, Revert & Reflog for Clean Code Management

stash

Description

Official definition: 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 changes and restores the working directory to match the HEAD commit.

Use Cases

When you are developing on a feature branch and an urgent bug fix is required on master, you cannot switch branches because of uncommitted changes. Stashing lets you temporarily store those changes, switch branches, fix the bug, and then reapply the stashed changes.

Command Usage

git stash

To restore the stashed changes after fixing the bug:

git stash apply

Related Commands

# Save current uncommitted changes
git stash

# Save with a message
git stash save "message"

# List all stash entries
git stash list

# Delete all stash entries
git stash clear

# Apply the most recent stash
git stash apply

# Apply and drop the most recent 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:

git stash apply stash@{1}

VS Code Integration

Stash commands are available in the VS Code UI under the STASHES menu, where you can add a message, view saved stashes, and apply or pop them.

reset --soft

Description

Resets the HEAD to a previous commit without touching the index or working tree, moving the changes back to the staging area. It effectively acts as a “soft” undo, preserving modifications for further editing.

Use Cases

1. Accidentally committed changes that should not have been included; you can undo the commit while keeping the changes staged.

2. Teams require granular, well‑described commits; if multiple unrelated changes were committed together, reset --soft lets you split them into separate commits.

Command Usage

git reset --soft HEAD^

For already pushed commits, you can reset locally and then force‑push with git push -f. When specifying a commit hash, git reset --soft <hash> moves all changes from that commit up to the current HEAD back to the staging area.

cherry-pick

Description

Copies one or more existing commits and creates new commits with the same changes on the current branch. The working tree must be clean before running the command.

Use Cases

1. A completed feature needs to be released urgently while other in‑progress work remains on the feature branch.

2. A development branch became polluted; you create a clean branch and cherry‑pick the desired commits from the old branch.

Command Usage

Single commit: git cherry-pick <commitHash> Multiple commits: git cherry-pick commit1 commit2 Range of consecutive commits:

git cherry-pick commit1^..commit2

Conflict Resolution

If conflicts occur during a multi‑commit cherry‑pick, the process stops. Resolve the conflicts, stage the changes, then continue with: 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

Creates a new commit that undoes the changes introduced by an existing commit (or commits). The working tree must be clean.

Use Cases

When a recently deployed feature causes a critical issue, git revert safely creates a rollback commit without rewriting history, preserving other teammates' work.

Command Usage

Reverting a regular commit:

git revert 21dcd937fe555f58841b17466a99118deb489212

Reverting a merge commit requires specifying the parent to keep (usually -m 1): git revert -m 1 <mergeCommitHash> After reverting a merge, subsequent merges of the same changes may be ignored because Git sees the commit as already applied. To re‑apply, revert the revert commit.

reflog

Description

Tracks updates to the tip of branches and other references, providing a safety net to recover lost commits after operations like reset --hard.

Use Cases

If you accidentally reset too far and lose commits, git reflog shows the previous HEAD positions. You can then reset back to the desired commit hash to restore the lost work.

Command Usage

git reflog

Identify the correct entry and reset:

git reset --hard <desiredHash>

Setting Git Short Commands

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

Use the short alias:

git cp <commitHash>

Summary

This article shares five practical Git commands— stash, reset --soft, cherry-pick, revert, and reflog —along with ways to create short aliases, helping developers manage code changes efficiently and recover from mistakes.

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.