Git Cheatsheet
Git Cheatsheet
Section titled “Git Cheatsheet”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 maingit config --list # view all configStarting a Repository
Section titled “Starting a Repository”git init # new repogit init my-project # new repo in directorygit clone https://github.com/u/repo.git # clone remotegit clone https://... my-folder # clone into custom directoryDaily Workflow
Section titled “Daily Workflow”git status # working tree statusgit status -s # short format
git add file.txt # stage filegit add . # stage everythinggit add -p # stage interactively (hunk by hunk)
git commit -m "message"git commit -am "message" # stage tracked + commit
git diff # unstaged changesgit diff --staged # staged changesBranches
Section titled “Branches”git branch # list local branchesgit branch -a # list all (incl. remote)git switch feature/login # switch branchgit switch -c feature/login # create + switchgit branch -d feature/login # delete (safe)git branch -D feature/login # force deletegit branch -m old-name new-name # renameMerging and Rebasing
Section titled “Merging and Rebasing”git merge feature/login # merge into current branchgit merge --no-ff feature/login # force merge commitgit merge --abort # abort in-progress merge
git rebase main # rebase current onto maingit rebase -i HEAD~3 # interactive rebase last 3git rebase --continue # after resolving conflictsgit rebase --abort # cancel rebaseRemotes
Section titled “Remotes”git remote -v # list remotesgit remote add origin https://... # add remotegit fetch origin # download (no merge)git pull # fetch + mergegit pull --rebase # fetch + rebase
git push origin main # push branchgit push -u origin main # push + set upstreamgit push --force-with-lease # safe force push (after rebase)git push origin --delete feature/old # delete remote branchHistory
Section titled “History”git loggit log --onelinegit log --oneline --graphgit log --oneline -10git log --author="Alice"git log --since="1 week ago"git log -- file.txt # commits touching a filegit show abc1234 # show a commitgit diff main feature/x # diff between branchesgit blame file.txt # who changed each linegit stash # stash changesgit stash push -m "description"git stash -u # include untrackedgit stash listgit stash pop # apply + dropgit stash apply stash@{1} # apply without droppinggit stash drop stash@{1}git stash clear # remove all stashesgit tag -a v1.0.0 -m "Release 1.0.0" # annotated taggit tag v1.0.0 # lightweight taggit tag -l # list tagsgit tag -l "v1.*" # filtergit push origin v1.0.0 # push taggit push origin --tags # push all tagsgit tag -d v1.0.0-rc1 # delete localgit push origin --delete v1.0.0-rc1 # delete remoteUndoing Changes
Section titled “Undoing Changes”git restore file.txt # discard working tree changesgit restore --staged file.txt # unstage filegit commit --amend -m "new message" # change last commit messagegit 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 stagedgit reset --mixed HEAD~1 # undo commit, keep in working treegit reset --hard HEAD~1 # undo commit, LOSE changesRecovery
Section titled “Recovery”git reflog # history of HEAD changesgit reset --hard HEAD@{3} # restore to reflog entrygit switch -c recovery-branch abc1234 # recover lost commit to branchUseful Shortcuts
Section titled “Useful Shortcuts”git cherry-pick abc1234 # apply commit to current branchgit bisect start # binary search for bug-introducing commitgit clean -fd # delete untracked files + dirsgit shortlog -sn # commit count per authorgit log --all --oneline --graph # full branch graphQuick Reference Table
Section titled “Quick Reference Table”| Action | Command |
|---|---|
| New branch + switch | git switch -c branch-name |
| Stage all changes | git add . |
| Amend last commit | git commit --amend |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Safe force push | git push --force-with-lease |
| Apply stash | git stash pop |
| Create annotated tag | git tag -a v1.0.0 -m "msg" |
| Revert a commit | git revert abc1234 |
| Show file at commit | git show abc1234:path/to/file.txt |