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
ITPUB
Master Advanced Git Commands: A Practical Cheat Sheet for Power Users

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/master

Compare Branch with Master

git diff master..my-branch

Customize 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 --autosquash

Run 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 --delete

GitHub Shortcuts (using hub)

# Treat hub as a wrapper for git
alias git='hub'

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

Useful 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.

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 Sheetaliasesadvanced commands
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.