Git Commands: Cheat Sheet
Setup
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global pull.rebase true
git config --global init.defaultBranch main
Branching
git switch -c feature/deploy-pipeline # Create and switch
git branch -a # List all branches
git branch --merged main # Branches merged into main
git branch -d feature/old # Safe delete
git branch -D feature/old # Force delete
git push origin --delete feature/old # Delete remote branch
git branch -m new-name # Rename current branch
Staging & Committing
git add file.tf modules/ # Stage specific files
git add -p # Stage hunks interactively
git commit -m "fix: correct subnet CIDR"
git commit --amend --no-edit # Amend last commit (before push)
git commit --allow-empty -m "ci: trigger pipeline"
Stashing
git stash # Stash working changes
git stash push -m "wip: iam policy" # Stash with message
git stash list # List stashes
git stash pop # Apply and drop latest stash
git stash apply stash@{0} # Apply without dropping
git stash drop stash@{1} # Drop specific stash
Rebase & Merge
git rebase main # Rebase onto main
git rebase --continue # After resolving conflicts
git rebase --abort # Cancel rebase
git rebase -i HEAD~3 # Squash last 3 commits
git merge --no-ff feature/deploy # Merge keeping branch history
Searching & Inspecting
git log --grep="deploy" # Search commit messages
git log -S "API_KEY" --oneline # Search code changes (pickaxe)
git blame path/to/file.yaml # Who changed each line
git show abc1234:path/to/file.yaml # File at specific commit
git diff main..feature/deploy -- path/ # Diff between branches
Undoing & Recovery
git restore --staged file.tf # Unstage a file
git restore file.tf # Discard working changes
git revert abc1234 # Revert commit (new commit)
git reset --soft HEAD~1 # Undo commit, keep staged
git reflog # Find lost commits
Bisect (Find Bad Commit)
git bisect start
git bisect bad # Current commit is broken
git bisect good v1.2.0 # This tag was working
# Git checks out middle — test, then: git bisect good / bad
git bisect reset # Done — reset
Tags
git tag -a v1.5.0 -m "Release 1.5.0" # Annotated tag
git tag -l "v1.*" # List tags
git push origin --tags # Push all tags
Remote & Sync
git remote -v # Show remotes
git fetch --all --prune # Fetch + prune deleted remotes
git pull --rebase origin main # Pull with rebase
git push -u origin feature/deploy # Push + set upstream
git push --force-with-lease # Safe force push
Handy Aliases
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate -20"
git config --global alias.undo "reset --soft HEAD~1"
Related Articles
Was this article helpful?
CI/CD Engineering Lead
Automation evangelist who believes no deployment should require a human. I write pipelines, break pipelines, and write about both. Code-first, always.
Related Articles
Git Bisect: Find the Exact Commit That Broke Production
Use git bisect to binary-search through commit history and pinpoint the exact change that introduced a bug — manually or fully automated.
Git Push Rejected: How To Fix "Updates Were Rejected Because The Remote Contains Work You Do Not Have"
You've seen this error. Everyone has. You try to push your changes and Git stops you cold: Here's your fast-track reference to diagnose and fix it. --- The...
Git Worktrees For Managing Multiple Feature Branches Simultaneously
Stop stashing. Stop context-switching. Git worktrees let you check out multiple branches simultaneously in separate directories — each with its own working...
Fix Git 'fatal: refusing to merge unrelated histories'
Resolve the Git error 'refusing to merge unrelated histories' when pulling or merging branches that share no common ancestor.
Linux Fundamentals: File System Navigation and Permissions
Navigate the Linux file system hierarchy, master essential commands, understand file permissions and ownership, and work with links, pipes, and redirection.
Pre-Commit Hooks Framework for Automated Code Quality
Set up the pre-commit hooks framework to automatically enforce linting, formatting, and security checks before every Git commit.