Skip to content

Git Cheatsheet

Terminal window
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --list # view all config
Terminal window
git init # new repo
git init my-project # new repo in directory
git clone https://github.com/u/repo.git # clone remote
git clone https://... my-folder # clone into custom directory
Terminal window
git status # working tree status
git status -s # short format
git add file.txt # stage file
git add . # stage everything
git add -p # stage interactively (hunk by hunk)
git commit -m "message"
git commit -am "message" # stage tracked + commit
git diff # unstaged changes
git diff --staged # staged changes
Terminal window
git branch # list local branches
git branch -a # list all (incl. remote)
git switch feature/login # switch branch
git switch -c feature/login # create + switch
git branch -d feature/login # delete (safe)
git branch -D feature/login # force delete
git branch -m old-name new-name # rename
Terminal window
git merge feature/login # merge into current branch
git merge --no-ff feature/login # force merge commit
git merge --abort # abort in-progress merge
git rebase main # rebase current onto main
git rebase -i HEAD~3 # interactive rebase last 3
git rebase --continue # after resolving conflicts
git rebase --abort # cancel rebase
Terminal window
git remote -v # list remotes
git remote add origin https://... # add remote
git fetch origin # download (no merge)
git pull # fetch + merge
git pull --rebase # fetch + rebase
git push origin main # push branch
git push -u origin main # push + set upstream
git push --force-with-lease # safe force push (after rebase)
git push origin --delete feature/old # delete remote branch
Terminal window
git log
git log --oneline
git log --oneline --graph
git log --oneline -10
git log --author="Alice"
git log --since="1 week ago"
git log -- file.txt # commits touching a file
git show abc1234 # show a commit
git diff main feature/x # diff between branches
git blame file.txt # who changed each line
Terminal window
git stash # stash changes
git stash push -m "description"
git stash -u # include untracked
git stash list
git stash pop # apply + drop
git stash apply stash@{1} # apply without dropping
git stash drop stash@{1}
git stash clear # remove all stashes
Terminal window
git tag -a v1.0.0 -m "Release 1.0.0" # annotated tag
git tag v1.0.0 # lightweight tag
git tag -l # list tags
git tag -l "v1.*" # filter
git push origin v1.0.0 # push tag
git push origin --tags # push all tags
git tag -d v1.0.0-rc1 # delete local
git push origin --delete v1.0.0-rc1 # delete remote
Terminal window
git restore file.txt # discard working tree changes
git restore --staged file.txt # unstage file
git commit --amend -m "new message" # change last commit message
git commit --amend --no-edit # add files to last commit
git revert HEAD # safe undo (creates new commit)
git revert abc1234 # revert specific commit
git reset --soft HEAD~1 # undo commit, keep staged
git reset --mixed HEAD~1 # undo commit, keep in working tree
git reset --hard HEAD~1 # undo commit, LOSE changes
Terminal window
git reflog # history of HEAD changes
git reset --hard HEAD@{3} # restore to reflog entry
git switch -c recovery-branch abc1234 # recover lost commit to branch
Terminal window
git cherry-pick abc1234 # apply commit to current branch
git bisect start # binary search for bug-introducing commit
git clean -fd # delete untracked files + dirs
git shortlog -sn # commit count per author
git log --all --oneline --graph # full branch graph
ActionCommand
New branch + switchgit switch -c branch-name
Stage all changesgit add .
Amend last commitgit commit --amend
Undo last commit (keep changes)git reset --soft HEAD~1
Safe force pushgit push --force-with-lease
Apply stashgit stash pop
Create annotated taggit tag -a v1.0.0 -m "msg"
Revert a commitgit revert abc1234
Show file at commitgit show abc1234:path/to/file.txt