Backend Development 6 min read
Master Advanced Git Commands: A Practical Cheat Sheet for Power Users
This guide provides a concise cheat sheet of advanced Git commands—including navigation, history inspection, resetting, rebasing, squashing, stashing, cleaning, and custom aliases—to help developers work more efficiently with version control.
ITPUB
ITPUB
Navigate – Jump to Previous Branch
git checkout -View History
# Show each commit on one line
git log --oneline
# Search all logs for a keyword
git log --all --grep='homepage'
# Show commits by a specific author
git log --author="Maxence"Undo an Unwanted Reset
# List reflog entries
git reflog
# Reset to a specific reflog entry
git reset HEAD@{4}
# Hard reset to a known commit hash
git reset --hard <code>COMMIT_HASH</code>Clean a Messy Local Repository
git fetch origin
git checkout master
git reset --hard origin/masterCompare Branch with Master
git diff master..my-branchCustomize Commits
# Amend the last commit message
git commit --amend -m "Better commit message"
# Add changes to the last commit without changing the message
git add . && git commit --amend --no-edit
# Create an empty commit (useful to trigger CI builds)
git commit --allow-empty -m "chore: re-trigger build"Squash Commits
To squash the last three commits:
git rebase -i HEAD~3
# Keep the first line as "pick", change the others to "squash" (or "s")
# Save and exit (e.g., :wq in vi)Fixup a Specific Commit
# Stage changes and mark them as fixup for the previous commit
git add .
git commit --fixup HEAD~1
# Rebase interactively with autosquash
git rebase -i HEAD~3 --autosquashRun a Command on Every Commit During Rebase
# Execute a test command on each of the last three commits
git rebase HEAD~3 --exec "npm test"Stash Changes
# Save all tracked files with a message
git stash save "log info"
# List 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}Clean Up Remote Branches
# Remove remote‑tracking branches that no longer exist on the server
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 Shortcuts (using hub)
# Treat 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'
# Force‑push shortcut
alias yolo='git push --force'
# Weekly stand‑up report 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"
}Original Source
Signed-in readers can open the original source through BestHub's protected redirect.
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 contactand we will review it promptly.
Written by
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
0 followers
Reader feedback
How this landed with the community
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
