Fundamentals 14 min read

Master Git Stash, Reset, Cherry-Pick, Revert & Reflog: Practical Commands

This guide walks developers through five essential Git commands—stash, reset --soft, cherry‑pick, revert, and reflog—explaining their purposes, real‑world scenarios, and step‑by‑step usage, plus tips for creating handy Git aliases to boost workflow efficiency.

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

Using Git as a version‑control system is a core skill for developers, yet many only know basic save, pull, and push operations. This article shares practical Git commands that greatly improve efficiency and solve common scenarios.

stash

Description

The git stash command records the current state of the working directory and index, then restores a clean working tree. It temporarily saves uncommitted changes.

Application Scenario

When a feature branch is in the middle of development and an urgent bug must be fixed on master, git stash lets you switch branches without committing incomplete work.

Command Usage

git stash

After fixing the bug, restore the saved changes:

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
# Clear all stash entries
git stash clear
# Apply the most recent stash
git stash apply
# Apply and delete the most recent stash
git stash pop
# Delete a specific stash
git stash drop

When multiple stashes exist, list them and apply a specific one:

git stash list
git stash apply stash@{1}

reset --soft

Description

git reset --soft

moves the HEAD to a previous commit while keeping all changes staged, effectively acting as a “soft undo” that preserves modifications.

Application Scenario

Accidentally committed unfinished work or need to amend a commit before pushing. --soft lets you modify the commit without losing changes.

Command Usage

git reset --soft HEAD^

To reset to a specific commit:

git reset --soft 1a900ac29eba73ce817bf959f82ffcb0bfa38f75

If the commit has already been pushed, force‑push the corrected history:

git push -f

cherry-pick

Description

git cherry-pick

applies the changes introduced by existing commits onto the current branch, creating new commits for each.

Application Scenario

When a specific feature or fix needs to be transferred to another branch without merging the entire branch history.

Command Usage

Copy a single commit: git cherry-pick <commitHash> Copy multiple commits: git cherry-pick commit1 commit2 Copy a range of commits: git cherry-pick commit1^..commit2 If conflicts occur, resolve them, then continue: 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

git revert

creates a new commit that undoes the changes introduced by a given commit (or range), preserving a clean history.

Application Scenario

Urgently roll back a problematic feature without affecting other teammates' work.

Command Usage

Revert a regular commit:

git revert 21dcd937fe555f58841b17466a99118deb489212

Revert a merge commit (specify the mainline parent):

git revert -m 1 <mergeCommitHash>

reflog

Description

git reflog

records updates to references, allowing you to recover lost commits after operations like a hard reset.

Application Scenario

After an accidental reset --hard that removed a colleague’s commit, use reflog to find the missing commit hash and restore it.

Command Usage

git reflog

Identify the desired entry, then reset back 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

Summary

stash

: temporarily store uncommitted changes. reset --soft: roll back commits while keeping changes staged. cherry-pick: copy specific commits to another branch. revert: create a new commit that undoes previous changes. reflog: view reference history to recover lost commits.

The examples illustrate real‑world scenarios and command sequences to help developers use these Git features effectively.

software developmentCommand Line
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.