DevOpsil
Git
82%
Fresh

Git Commands: Cheat Sheet

Sarah ChenSarah Chen3 min read

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
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"
Share:
Sarah Chen
Sarah Chen

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