Master Advanced Git Tricks: Navigation, Rebase, Stash, and More
This cheat sheet covers advanced Git techniques such as jumping to previous branches, viewing commit history, resetting and cleaning up branches, customizing and squashing commits, interactive rebasing with exec, powerful stash commands, remote cleanup, and handy GitHub aliases, all illustrated with concise examples.
Navigation – Jump to Previous Branch
git checkout -Viewing History
# Show each commit on one line
git log --oneline
# Search all commit logs for "homepage"
git log --all --grep='homepage'
# Get commits by a specific author
git log --author="Maxence"Undoing and Resetting
# Show all operation history
git reflog
# Reset to a specific reflog entry
git reset HEAD@{4}
# Hard reset to a specific commit hash
git reset --hard <commit-hash>Cleaning a Messy Local Repository
git fetch origin
git checkout master
git reset --hard origin/masterComparing Branches
git diff master..my-branchCustomizing Commits
# Amend the last commit with a new message
git commit --amend -m "Better commit message"
# Add changes and amend without editing the message
git add . && git commit --amend --no-edit
# Create an empty commit to re‑trigger CI builds
git commit --allow-empty -m "chore: re-trigger build"Squashing Commits
# Interactive rebase of the last 3 commits
git rebase -i HEAD~3
# In the editor, keep the first line as "pick" and change the rest to "squash" (or "s")
# Then save and exit (e.g., :wq in vi) # Example rebase todo list
pick 64d26a1 fea: add index.js
s 45f0259 fix: update index.js
s 8b15b0a fix: typo in index.jsRebase with Autosquash
git rebase -i HEAD~3 --autosquashRun a Command on Every Commit (rebase --exec)
# Run npm test on the last 3 commits
git rebase HEAD~3 --exec "npm test"Stashing
# Save all tracked files with a message
git stash save "log message"
# List all stash entries
git stash list
# Apply and drop a specific stash
git stash apply stash@{1}
git stash drop stash@{1}
# Or pop in one step
git stash pop stash@{1}Cleaning Remote Branches
# Remove branches that no longer exist on the remote
git fetch -p
# Delete all remote branches containing "greenkeeper"
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.{9}//' | xargs git push origin --deleteGitHub Alias
# Use hub as a wrapper for git
alias git='hub'
# Open the repository URL in a browser (GitHub only)
git browseUseful 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'
alias yolo='git push --force'
# Weekly stand‑up function
function 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"
}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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
