Fundamentals 13 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, reflog—explains their typical scenarios, provides step‑by‑step usage examples with code snippets, and shows how to configure short aliases to boost development efficiency.

IT Services Circle
IT Services Circle
IT Services Circle
Practical Git Commands: stash, reset --soft, cherry‑pick, revert, reflog and Short Aliases

Introduction

Git is a must‑know tool for developers, yet many only master the basics of pull, push, and commit. This guide shares practical commands that can dramatically improve workflow and solve common problems.

stash

Description

Stash saves uncommitted changes and restores a clean working directory.

Use Cases

When you need to switch branches urgently (e.g., to fix a production bug) but have unfinished work, stash lets you store the changes without committing them.

Command Usage

git stash

To apply the saved changes later:

git stash apply

Related Commands

# Save current uncommitted changes
git stash

# Save with a message
git stash save "
"

# 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 the most recent stash
git stash drop

reset --soft

Description

Reset --soft moves the HEAD to a previous commit while keeping the changes staged, effectively acting as a “soft undo”.

Use Cases

Useful when you accidentally commit unwanted changes and want to amend them without creating extra history.

Command Usage

git reset --soft HEAD^

After resetting, you can modify the changes and recommit. For already‑pushed commits, you need to force‑push:

git push -f

cherry‑pick

Description

Cherry‑pick copies one or more existing commits and creates new commits on the current branch.

Use Cases

When a specific feature or bug‑fix needs to be applied to another branch without merging the whole branch.

Command Usage

Copy a single commit:

git cherry-pick

Copy multiple commits:

git cherry-pick commit1 commit2

Copy a range of commits:

git cherry-pick commit1^..commit2

Handling Conflicts

If conflicts occur, resolve them, stage the changes, and continue:

git cherry-pick --continue

To abort or quit the operation:

git cherry-pick --abort
git cherry-pick --quit

revert

Description

Revert creates a new commit that undoes the changes introduced by a previous commit.

Use Cases

When a released feature causes issues and you need to roll back its changes without affecting other commits.

Command Usage

git revert

To revert a merge commit, specify the parent:

git revert -m 1

reflog

Description

Reflog records all movements of HEAD, allowing you to recover lost commits after mistaken resets.

Use Cases

If you accidentally reset too far and lost a commit, reflog lets you find the missing commit hash and restore it.

Command Usage

git reflog

Find the desired commit hash from the reflog output and reset back to it:

git reset --hard

Setting Short Git Aliases

Method 1

git config --global alias.ps push

Method 2

Edit the global config file (~/.gitconfig) and add aliases:

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

Now you can run git cp <commitHash> as a shortcut for git cherry-pick .

Conclusion

The article covered five practical Git commands—stash, reset --soft, cherry‑pick, revert, reflog—and demonstrated how to create short aliases to speed up development.

gitversion controlresetcherry-pickstashrevertreflog
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login 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.