Git Cheat Sheet
NEWgit · reference · commands
Setup
git config --global user.name "Name"Set your global username
git config --global user.email "email"Set your global email
git config --global core.editor "code --wait"Set VS Code as default editor
git config --listList all configuration settings
Initialize
git initInitialize a new local repository
git clone <url>Clone a remote repository
git clone <url> <dir>Clone into a specific directory
Stage & Snapshot
git statusShow working tree status
git add <file>Stage a specific file
git add .Stage all changes
git add -pInteractively stage hunks
git diffShow unstaged changes
git diff --stagedShow staged changes
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit
git commit --amendAmend the last commit
Branch & Merge
git branchList local branches
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git checkout -b <branch>Create and switch to new branch
git switch <branch>Switch branches (modern syntax)
git switch -c <branch>Create and switch (modern)
git merge <branch>Merge branch into current
git branch -d <branch>Delete a branch
git branch -D <branch>Force delete a branch
Remote
git remote -vList remotes
git remote add origin <url>Add a remote
git fetchFetch all remotes
git pullFetch and merge from remote
git pull --rebaseFetch and rebase from remote
git push origin <branch>Push branch to remote
git push -u origin <branch>Push and set upstream
git push --force-with-leaseSafe force push
Undo & Reset
git revert <commit>Create revert commit
git reset HEAD <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep staged
git reset --hard HEAD~1Undo last commit, discard changes
git checkout -- <file>Discard file changes
git clean -fdRemove untracked files/dirs
Stash
git stashStash working directory changes
git stash listList all stashes
git stash popApply latest stash and remove
git stash apply stash@{n}Apply specific stash
git stash drop stash@{n}Delete a stash
Log & Inspect
git log --onelineCompact commit history
git log --oneline --graph --allVisual branch graph
git log --author="Name"Filter commits by author
git show <commit>Show commit details
git blame <file>Show who changed each line
git diff <branch1>..<branch2>Diff between branches