/git-cheatsheetGit Cheatsheet
Quick reference for common Git commands
git initInitialize a new repository
git init my-project
git cloneClone a remote repository
git clone https://github.com/user/repo.git
git remote addAdd a remote
git remote add origin https://github.com/user/repo.git
git remote -vList remotes
git remote -v
git branchList branches
git branch
git branch <name>Create a branch
git branch feature-x
git checkout <name>Switch to branch
git checkout main
git checkout -b <name>Create and switch
git checkout -b feature-x
git switch <name>Switch branch (new)
git switch main
git switch -c <name>Create and switch
git switch -c feature-x
git branch -d <name>Delete branch
git branch -d old-branch
git branch -D <name>Force delete branch
git branch -D unwanted
git merge <name>Merge branch into current
git merge feature-x
git add <file>Stage a file
git add src/index.ts
git add .Stage all changes
git add .
git commit -mCommit with message
git commit -m 'fix: typo'
git commit -amAdd+commit tracked files
git commit -am 'update'
git commit --amendAmend last commit
git commit --amend -m 'new msg'
git statusShow working tree status
git status
git pushPush to remote
git push origin main
git push -u <remote> <branch>Set upstream and push
git push -u origin main
git push --forceForce push (careful!)
git push --force
git pullPull from remote
git pull origin main
git fetchFetch without merging
git fetch origin
git merge <branch>Merge branch
git merge feature-x
git merge --abortAbort a merge
git merge --abort
git rebase <branch>Rebase onto branch
git rebase main
git rebase -i HEAD~nInteractive rebase
git rebase -i HEAD~3
git rebase --abortAbort a rebase
git rebase --abort
git rebase --continueContinue after resolving
git rebase --continue
git stashStash working changes
git stash
git stash popApply and drop stash
git stash pop
git stash listList stashes
git stash list
git stash applyApply without dropping
git stash apply stash@{0}
git stash dropDrop a stash
git stash drop stash@{0}
git stash clearClear all stashes
git stash clear
git reset <file>Unstage a file
git reset src/index.ts
git reset --soft HEAD~1Undo commit, keep changes
git reset --soft HEAD~1
git reset --mixed HEAD~1Undo commit, unstage
git reset --mixed HEAD~1
git reset --hard HEAD~1Undo commit, discard changes
git reset --hard HEAD~1
git revert <commit>Revert a commit (safe)
git revert abc123
git diffShow unstaged changes
git diff
git diff --stagedShow staged changes
git diff --staged
git diff <branch>Compare with branch
git diff main
git diff <commit> <commit>Compare commits
git diff abc123 def456
git logShow commit history
git log
git log --onelineCompact log
git log --oneline -10
git log --graphShow branch graph
git log --oneline --graph --all
git log -pShow patches
git log -p file.ts
git blame <file>Show who changed each line
git blame src/index.ts
git config --global user.nameSet user name
git config --global user.name 'Alice'
git config --global user.emailSet user email
git config --global user.email 'alice@example.com'
git config --listList all config
git config --list
git config --global alias.<name>Create alias
git config --global alias.co checkout
.gitignoreIgnore files
node_modules/\n.env