Git Advanced: The Commands I Wish I Knew Earlier (2026)
Beyond add, commit, push. These are the Git commands that make you significantly more productive.
Rewriting History (Use with Care!)
# Modify your last commit (before pushing!)
git commit --amend -m "Better message"
# Add forgotten file to last commit:
git add forgotten-file.js
git commit --amend --no-edit # Keep same message, add the file
# Interactive rebase — edit/squash/reorder past commits
git rebase -i HEAD~5 # Edit last 5 commits
# Opens editor with:
# pick abc123 First commit
# pick def456 Second commit
# pick ghi789 Third commit
#
# Change 'pick' to:
# reword = Edit commit message
# squash = Merge into previous commit
# fixup = Merge into previous (discard message)
# edit = Pause for amending
# drop = Remove commit entirely
# Squash last 3 commits into one (non-interactive)
git reset --soft HEAD~3 && git commit -m "Combined message"
# Drop a specific commit from history
git rebase -i <commit-before-target> # Then change 'pick' to 'drop'
Stashing: Save Work Without Committing
# Basic stash (saves all changes, including untracked if -u)
git stash # Stash tracked changes
git stash -u # Also stash untracked files
git stash -p # Interactively choose what to stash
# List and apply stashes
git stash list # Shows all stashes
# stash@{0}: WIP on feature-branch: abc1234 message
# stash@{1}: WIP on main: def5678 other work
git stash pop # Apply most recent + remove from list
git stash apply # Apply but keep in list
git stash apply stash@{1} # Apply specific stash
# Create named stash for easy reference
git stash save "WIP: login feature"
# Show what's in a stash before applying
git stash show -p stash@{0} # See the actual diff!
# Drop/clear stashes
git stash drop stash@{0} # Remove one
git stash clear # Remove all stashes
# Practical workflow:
# You're working on feature A → urgent bug report comes in
git stash -u # Save all work
git checkout -b hotfix/bug # Switch to fix branch
# ... fix bug, commit, push ...
git checkout feature-a # Back to original work
git stash pop # Restore exactly where you were
Searching Through History
# Search commit messages
git log --grep="login" # Commits mentioning "login"
git log --grep="auth" --oneline --all # All branches, compact
# Search CODE content across all history
git log -S "functionName" --oneline # "Pickaxe" — find when string was added/removed
git log -G "pattern" --oneline # Find when regex pattern changed
# Search by author/date
git log --author="John" --since="2026-01-01" --until="2026-03-01"
git log --after="last week" --before="yesterday"
# Find which commit broke something (BINARY SEARCH!)
git bisect start
git bisect bad # Current version is broken
git bisect good v2.0 # This version worked
# Git checks out a middle commit automatically
# You test it, then run:
git bisect good # This commit works
git bisect bad # This commit is broken
# Repeat until Git finds the exact breaking commit!
git bisect reset # Done, go back to original branch
# Find a file's history
git log --follow -- file.txt # Track across renames!
git log -p --follow file.txt # Show full diffs too
# Who changed this line?
git blame file.txt # Show who wrote each line
git blame -L 10,20 file.txt # Only lines 10-20
Branching & Merging Mastery
# Create branch from specific point
git branch feature-x abc1234 # Branch starting at commit abc1234
# Rename branches
git branch -m old-name new-name
git branch -m new-name # Rename current branch
# Compare branches
git diff main...feature # Changes since branches diverged
git diff main..feature # Tip of main vs tip of feature
git log main..feature # Commits in feature not in main
git log main...feature # Symmetric difference (both ways)
# Clean up merged branches
git branch --merged # Branches already merged into current
git branch --merged | grep -v '\*' | xargs git branch -d # Delete them
# Recover deleted branch
git reflog # Find where the branch pointed
git checkout -b recovered-branch abc1234 # Recreate from SHA
# Cherry-pick specific commits
git cherry-pick abc123 # Apply one commit from another branch
git cherry-pick abc123 def456 # Multiple commits
git cherry-pick --no-commit abc123 # Apply changes without committing
# Merge strategies
git merge feature # Default (fast-forward or merge commit)
git merge --no-ff feature # Always create merge commit (preserves history)
git merge --squash feature # Squash all commits into one (no merge commit)
Working with Remotes
# See all remotes and their URLs
git remote -v
git remote show origin # Detailed info about origin
# Fetch without merging
git fetch origin # Get all updates
git fetch --prune origin # Remove local refs for deleted remote branches
# Push to different remote/branch
git push origin feature # Push current branch to origin/feature
git push origin HEAD:main # Push current branch TO main on remote
git push -f origin feature # Force push (⚠️ dangerous on shared branches!)
# Track a new remote branch
git checkout --track origin/new-feature
# Or shorter:
git checkout origin/new-feature
# Remove remote branch
git push origin --delete feature-branch
# Or:
git push origin :feature-branch
# Pull vs fetch + merge
git pull # fetch + merge (or rebase if configured)
git pull --rebase # fetch + rebase (cleaner linear history)
git pull --ff-only # Only fast-forward (rejects if would need merge)
# Sync with upstream (fork workflow)
git fetch upstream
git merge upstream/main # Integrate upstream changes
Troubleshooting Common Messes
# ❌ Committed to wrong branch
git reset --soft HEAD~1 # Undo commit, keep changes staged
git checkout correct-branch
git commit -c ORIG_HEAD # Reuse original commit message
# ❌ Accidentally pushed sensitive data (password, API key)
# Step 1: Remove from history IMMEDIATELY
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch secrets.env' \
--prune-empty --tag-name-filter cat -- --all
# Step 2: Force push (warn team first!)
git push origin --force --all
# Step 3: Rotate ALL compromised credentials immediately
# Better tool: BFG Repo Cleaner (faster than filter-branch)
# ❌ Merge conflict hell
# Don't panic. Systematic approach:
git status # See conflicted files
git diff # See the conflicts marked up
# Open files, look for: <<<<<<< HEAD, =======, >>>>>>> feature
# Edit to keep what you want, remove markers
git add resolved-file.js # Stage the resolution
git commit # Complete the merge
# Use a merge tool for complex conflicts:
git mergetool # Opens configured visual merge tool
# Abort if overwhelmed
git merge --abort # Go back to pre-merge state
# ❌ Detached HEAD state (made commits while detached)
git checkout main # Switch back (commits still there!)
git reflog # Find your detached commits
git cherry-pick abc1234 # Bring them to a real branch
# ❌ Need to undo something?
git restore file.txt # Discard working directory changes
git restore --staged file.txt # Unstage (keep changes)
git reset --hard HEAD~1 # Destroy last commit AND changes (⚠️ irreversible!)
git revert abc123 # New commit that undoes abc123 (safe for shared!)
Useful Aliases to Add Today
# Add these to ~/.gitconfig [alias] section:
[alias]
co = checkout
br = branch
ci = commit
st = status -sb
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
unstage = reset HEAD --
amend = commit --amend --no-edit
save = stash push -u
pop = stash pop
prune-all = !git branch --merged | grep -v '\\*|main|develop' | xargs git branch -d
cleanup = !git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
recent = branch --sort=-committerdate -v --list
Which Git command here is new to you? What's your favorite Git trick that wasn't included?
Follow @armorbreak for more practical developer guides.
Top comments (0)