Git Cheat Sheet

The Git commands developers reach for every day, grouped by task.

Setup

git initCreate a new repository
git clone <url>Clone a remote repository
git config --global user.name "Name"Set your name

Staging & committing

git statusShow working tree status
git add <file>Stage a file (use . for all)
git commit -m "msg"Commit staged changes
git commit -am "msg"Stage tracked files and commit

Branches

git branchList branches
git checkout -b <name>Create and switch to a branch
git switch <name>Switch branches
git merge <name>Merge a branch into current

Remotes

git remote -vList remotes
git pullFetch and merge from remote
git pushPush commits to remote
git push -u origin <branch>Push and set upstream

Undoing

git restore <file>Discard working changes
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit and discard changes
git revert <hash>Create a commit that undoes another

Inspecting

git log --onelineCompact commit history
git diffUnstaged changes
git diff --stagedStaged changes
git show <hash>Show a commit

More cheat sheets