Fundamentals 14 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—explains their typical use cases, provides step‑by‑step examples, and shows how to create convenient short aliases to boost development efficiency.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Practical Git Commands: stash, reset --soft, cherry-pick, revert, reflog, and Short Aliases

Using Git for source code version control is a core skill for developers, yet many only know basic operations. This article shares practical Git commands that improve workflow, explains when to use them, and demonstrates how to configure short aliases.

stash

Description : Saves the current state of the working directory and index without committing, allowing you to return to a clean workspace.

Use case : When you are halfway through a feature and need to switch branches to fix an urgent bug.

Command : git stash To apply the saved changes later: git stash apply Additional useful stash commands:

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

reset --soft

Description : Moves the HEAD to a previous commit while keeping all changes staged, effectively acting as a "soft" undo.

Use case : You accidentally committed changes that should not have been included, or you need to split a large commit into smaller, logical ones.

Command : git reset --soft HEAD^ After resetting, you can amend the changes and create a new, clean commit.

cherry-pick

Description : Applies the changes introduced by existing commits onto the current branch, creating new commits for each.

Use case : You need to copy a specific feature or bug‑fix commit from one branch to another without merging the whole branch.

Single commit : git cherry-pick <commitHash> Multiple commits : git cherry-pick commit1 commit2 Or a range: git cherry-pick commit1^..commit2 If conflicts occur, resolve them, 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 a previous commit, keeping the history intact.

Use case : A recently deployed feature causes a critical bug and must be rolled back without losing other teammates' work.

Reverting a normal commit :

git revert 21dcd937fe555f58841b17466a99118deb489212

Reverting a merge commit (specify the mainline parent):

git revert -m 1 <mergeCommitHash>

reflog

Description : Records updates to the tip of branches and other references, allowing you to recover lost commits.

Use case : After an accidental hard reset that removed recent commits, you can locate the lost commit hash via reflog and restore it.

Command : git reflog Identify the desired entry and reset back to it, e.g.:

git reset --hard <commitHash>

Setting Git Short Aliases

Two ways to create short commands: git config --global alias.ps push Or edit ~/.gitconfig and add:

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

Now git cp <commitHash> works like git cherry-pick <commitHash>.

Summary

The article covers five practical Git commands—stash, reset --soft, cherry-pick, revert, reflog—and demonstrates how to configure short aliases, providing developers with concrete examples and scenarios to handle everyday version‑control challenges efficiently.

DevelopmentworkflowGitcommand-lineversion-control
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.