Master Advanced Git Commands: A Handy Cheat Sheet for Power Users
This cheat sheet gathers essential advanced Git commands—navigation, history inspection, commit amendment, interactive rebasing, stash management, branch cleanup, and useful aliases—providing concise examples and explanations to help developers work more efficiently with version control.
Navigate – Switch to the previous branch
Use git checkout - to quickly return to the branch you were on before the current checkout.
Inspect commit history
Common ways to view history:
# One‑line summary per commit
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"Recover a reset commit
If you have reset a commit and need to restore it, first list the reflog to find the lost commit, then reset to that entry.
# Show the reflog (record of HEAD moves)
git reflog
# Reset HEAD to a specific reflog entry, e.g., the 5th entry
git reset HEAD@{4}
# Or reset directly to a known commit hash
git reset --hard <commit-hash>Clean a messy local repository
Synchronise your local branch with the remote state, discarding local changes.
git fetch origin
git checkout master
git reset --hard origin/masterCompare a feature branch with master
Show the diff between master and another branch.
git diff master..my-branchCustomize the most recent commit
Amend commit messages, add additional changes, or create an empty commit to trigger CI pipelines.
# Change the message of the last commit
git commit --amend -m "Better commit message"
# Add new changes to the previous commit without editing the message
git add . && git commit --amend --no-edit
# Create an empty commit (useful for CI rebuilds)
git commit --allow-empty -m "chore: re-trigger build"Squash recent commits with interactive rebase
Combine several recent commits into one.
# Start an interactive rebase of the last 3 commits
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) to rewrite the history.Fixup a specific earlier commit
Mark new changes as a fixup for a previous commit and let --autosquash combine them automatically.
# Stage the changes
git add .
# Create a fixup commit targeting the commit two steps back
git commit --fixup HEAD~1
# Rebase with autosquash to merge the fixup automatically (adjust the range as needed)
git rebase -i HEAD~3 --autosquashRun a command on each commit during a rebase
Execute a test or any command for every commit in a range, aborting the rebase if the command fails.
# Run "npm test" on the last three commits
git rebase HEAD~3 --exec "npm test"Stash utilities
Temporarily store uncommitted changes and restore them later.
# Save all tracked files with a descriptive message
git stash save "log info"
# List all stash entries
git stash list
# Apply a specific stash without removing it
git stash apply stash@{1}
# Remove that stash entry
git stash drop stash@{1}
# Or apply and drop in one step
git stash pop stash@{1}Clean up remote-tracking branches
Remove references to branches that no longer exist on the remote, and optionally delete remote branches matching a pattern.
# Prune stale remote‑tracking branches
git fetch -p
# Delete all remote branches whose names contain "greenkeeper"
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.{9}//' | xargs git push origin --deleteGitHub shortcuts
Use the hub wrapper to add GitHub‑specific shortcuts.
# Alias the git command to hub (install hub first)
alias git='hub'
# Open the repository page in the default browser (GitHub only)
git browseUseful Git aliases
Define short aliases to speed up everyday workflows.
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'
alias yolo='git push --force'
# Weekly stand‑up helper function
git-standup() {
AUTHOR=${AUTHOR:="$(git config user.name)"}
since=yesterday
[[ $(date +%u) == 1 ]] && since="2 days ago"
git log --all --since "$since" --oneline --author "$AUTHOR"
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
