Fundamentals 7 min read

Master Advanced Git Tricks: Navigation, Rebase, Stash, and Cleanup Cheat Sheet

This cheat sheet compiles essential advanced Git commands—branch navigation, log inspection, reset recovery, diff comparison, commit customization, squash/fixup, rebase with exec, stash management, repository cleanup, and handy aliases—to help developers work more efficiently and avoid common pitfalls.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Advanced Git Tricks: Navigation, Rebase, Stash, and Cleanup Cheat Sheet

Navigation – Jump to previous branch

git checkout -

View commit history

# Show each commit on a single line
git log --oneline

# Search all commits for a keyword (e.g., "homepage")
git log --all --grep='homepage'

# Show commits authored by a specific person
git log --author="Maxence"

Undo an unwanted reset

# List all reflog entries (each action you performed)
git reflog

# Reset the HEAD to a specific reflog entry, e.g., the 4th entry back
git reset HEAD@{4}

# Or hard‑reset directly to a known commit hash
git reset --hard <commit-hash>

Clean a messy local repository

# Fetch the latest state from the remote
git fetch origin

# Switch to the main branch (commonly "master" or "main")
git checkout master

# Reset the local branch to exactly match the remote
git reset --hard origin/master

Compare a feature branch with the main branch

git diff master..my-branch

Customize commits

# Amend the most recent commit with a new message
git commit --amend -m "Better commit message"

# Add new changes to the previous commit without altering its message
git add . && git commit --amend --no-edit

# Create an empty commit (useful to re‑trigger CI pipelines)
git commit --allow-empty -m "chore: re‑trigger build"

Squash commits (interactive rebase)

To combine the last three commits into a single one:

git rebase -i HEAD~3
# In the editor, keep the first line as "pick" and change the others to "squash" (or "s")
# Save and quit (e.g., :wq in vi)

Fixup a specific commit

# Stage the changes you want to add to the target commit
git add .

# Mark the new changes as a fix‑up for the commit two steps back
git commit --fixup HEAD~1   # you can also use a commit hash instead of HEAD~1

# Run an interactive rebase that automatically squashes the fix‑up
git rebase -i HEAD~3 --autosquash   # then save and quit

Run a command on each commit during a rebase

If a test suite fails and you need to locate the offending commit, execute a command for every commit in the range:

# Run "npm test" on each of the last three commits
git rebase HEAD~3 --exec "npm test"

Stash management

# Save all tracked changes with a descriptive message
git stash save "log info"

# List all stash entries
git stash list

# Apply a specific stash and then drop it from the stash list
git stash apply stash@{1}
git stash drop stash@{1}

# Or apply and drop in a single step
git stash pop stash@{1}

Repository cleanup

# Remove remote-tracking branches that no longer exist on the server
git fetch -p

# Delete all remote branches whose name contains "greenkeeper"
# (fetch -p first to prune, then list, filter, and delete)
git fetch -p && git branch --remote | fgrep greenkeeper | \
  sed 's/^.{9}//' | xargs git push origin --delete

GitHub shortcut using hub alias

Treat hub as a thin wrapper around Git to add GitHub‑specific commands:

# Make "hub" the default Git command
alias git='hub'

# Open the repository page in a web browser (GitHub only)
git browse

Handy Git aliases

alias g='git'
alias glog='git log --oneline --decorate --graph'
alias gst='git status'
alias gp='git push'
alias ga='git add'
alias gc='git commit -v'
# Force‑push shortcut
alias yolo='git push --force'

# Weekly stand‑up report helper
git-standup() {
  AUTHOR=${AUTHOR:="$(git config user.name)"}
  since=yesterday
  if [[ $(date +%u) == 1 ]]; then
    since="2 days ago"
  fi
  git log --all --since "$since" --oneline --author="$AUTHOR"
}
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.

GitrebaseVersion ControlstashCheat Sheet
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.