Fundamentals 14 min read

Master Git: 5 Essential Commands to Boost Your Workflow

This article introduces five practical Git commands—stash, reset --soft, cherry-pick, revert, and reflog—explaining their purpose, typical scenarios, and step‑by‑step usage, plus tips for creating short aliases to streamline everyday version‑control tasks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Git: 5 Essential Commands to Boost Your Workflow

Git stash

Description

Temporarily saves the current working‑tree and index state, leaving a clean directory so you can switch branches without committing.

Typical scenario

While developing on a feature branch you discover an urgent bug on master. You have uncommitted changes that block git checkout master. Stashing lets you set those changes aside, fix the bug, then restore them.

Common commands

# Save current changes
git stash

# Save with a message
git stash save "my note"

# List all stash entries
git stash list

# 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 stash@{1}

# Clear all stash entries
git stash clear

Applying a specific stash

List the entries, then apply the desired one:

git stash list
# Example output
# stash@{0}: WIP on feature
# stash@{1}: WIP on feature

# Apply the second entry
git stash apply stash@{1}

Git reset --soft

Description

Moves HEAD to a previous commit while leaving all changes from the undone commits staged (in the index). It is effectively a “soft undo” that preserves the work for re‑commit.

Typical scenario

You accidentally committed changes that should have been split into smaller, logical commits, or you need to amend a recent commit without losing the modifications.

Usage

# Undo the most recent commit but keep changes staged
git reset --soft HEAD^

If the reset commit has already been pushed, force‑push the rewritten history: git push -f Note: git reset --soft <commit> restores **all** changes from the specified commit up to the current HEAD, not only the single commit.

Git cherry-pick

Description

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

Typical scenarios

Port a specific bug‑fix or feature from a development branch to master without merging the whole branch.

Extract clean commits from a polluted branch to a fresh branch before a release.

Single commit

# Identify the commit hash on the source branch
# Switch to the target branch
git checkout master
# Apply the commit
git cherry-pick <commitHash>

Multiple commits

# List of individual commits
git cherry-pick commit1 commit2

# Continuous range (inclusive)
git cherry-pick commit1^..commit2

Conflict handling

If a conflict occurs, Git pauses the operation. Resolve the conflicts, stage the changes, then continue: git cherry-pick --continue To abort the entire cherry‑pick sequence: git cherry-pick --abort To quit while keeping already applied commits:

git cherry-pick --quit

Git revert

Description

Creates a new commit that undoes the changes introduced by a specified commit, preserving history. The working tree must be clean.

Typical scenario

When a recently deployed change causes a critical issue, you can revert that commit without rewriting shared history.

Reverting a regular commit

git revert 21dcd937fe555f58841b17466a99118deb489212

Git opens an editor for the revert commit message; save and exit to complete.

Reverting a merge commit

Because a merge has two parents, you must specify which parent is the mainline (usually 1) with -m: git revert -m 1 <mergeCommitHash> Reverting a merge does not prevent the same merge from being re‑applied later; you can revert the revert if you need to re‑introduce the changes.

Git reflog

Description

Records updates to the tip of branches and HEAD, allowing you to recover commits that were lost due to mistaken resets or other destructive operations.

Typical scenario

You accidentally reset too far and cannot find the lost commit hash. git reflog shows the previous positions of HEAD so you can locate the missing hash.

Usage

# Show the reflog
git reflog

# Identify the desired entry (e.g., abc123)
# Reset back to that commit
git reset --hard abc123

Setting short Git aliases

Method 1 – Direct configuration

git config --global alias.ps push
git config --global alias.co checkout
git config --global alias.pl pull
git config --global alias.mer "merge --no-ff"
git config --global alias.cp cherry-pick

Method 2 – Editing ~/.gitconfig

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

Use the alias like any other Git command, e.g.:

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

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowsoftware developmentGitcommand-lineVersion Control
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.