elefcode
← All cheat sheets

Tools

Git Cheat Sheet — Essential Commands Reference

The Git commands that cover almost all everyday work, grouped by what you are trying to do.

Advertisement

Setup & config

git config --global user.name "Name"Set your commit name
git config --global user.email "you@example.com"Set your commit email
git config --listShow all current settings

Starting a repo

git initCreate a new repository here
git clone <url>Copy a remote repository locally
git remote -vList configured remotes
git remote add origin <url>Attach a remote named origin

Everyday snapshotting

git statusShow changed and staged files
git add <file>Stage a specific file
git add .Stage everything in the current directory
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching & merging

git branchList local branches
git switch -c <name>Create and switch to a new branch
git switch <name>Switch to an existing branch
git merge <branch>Merge a branch into the current one
git rebase <branch>Replay your commits on top of another branch
git branch -d <name>Delete a merged branch

Syncing with a remote

git fetchDownload remote changes without merging
git pullFetch and merge remote changes
git pushUpload your commits to the remote
git push -u origin <branch>Push and set the upstream branch

Undoing things

git restore <file>Discard local changes to a file
git restore --staged <file>Unstage a file, keeping the changes
git commit --amendRewrite the most recent commit
git revert <commit>Create a new commit undoing an old one (safe)
git reset --hard <commit>Discard commits and changes (destructive)

History & stashing

git log --oneline --graphCompact visual history
git show <commit>Show the contents of a commit
git blame <file>See who last changed each line
git stashShelve your uncommitted changes
git stash popReapply the most recently stashed changes

More cheat sheets