Git Commands Cheat Sheet
Short answer: a typical Git day is git status, git add, git commit, git pull and git push. This sheet groups the rest of the commands by what you are trying to do, so you can find the right one quickly.
Setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init # start a repository in the current folder
git clone URL # copy a remote repository
Daily workflow
| Command | Purpose |
|---|---|
git status | Show changed and staged files |
git add file / git add . | Stage a file / everything |
git add -p | Stage changes piece by piece |
git commit -m "message" | Save staged changes as a commit |
git diff / git diff --staged | See unstaged / staged changes |
git pull | Fetch and merge remote changes |
git push | Upload your commits |
Branches
| Command | Purpose |
|---|---|
git branch | List local branches |
git switch -c feature-x | Create and switch to a branch |
git switch main | Switch branches |
git merge feature-x | Merge a branch into the current one |
git branch -d feature-x | Delete a merged branch |
git rebase main | Replay your commits on top of main |
Older tutorials use git checkout for switching; git switch is clearer. Compare strategies in Git branching strategies and merge vs rebase.
Remotes
git remote -v # list remotes
git remote add origin URL # connect a remote
git fetch # download without merging
git push -u origin main # first push, set upstream
git push origin --delete old-branch # delete a remote branch
Inspect history
git log --oneline --graph --decorate --all
git log -p file.txt # changes to one file
git show abc1234 # one commit
git blame file.txt # who changed each line
git reflog # history of where HEAD has been
Undo things safely
| Situation | Command | Safe? |
|---|---|---|
| Unstage a file | git restore --staged file | Yes |
| Discard local edits to a file | git restore file | Loses uncommitted edits |
| Fix the last commit message | git commit --amend | Only if not pushed |
| Undo a commit by adding a new one | git revert abc1234 | Yes, safe for shared branches |
| Move back, keep changes | git reset --soft HEAD~1 | Yes |
| Move back, delete changes | git reset --hard HEAD~1 | Destructive |
If you lose a commit, git reflog often lets you find and recover it.
Stash: save work temporarily
git stash push -m "wip login"
git stash list
git stash pop # reapply and remove the latest stash
Details in Git stash.
Tags
git tag v1.0.0
git push origin v1.0.0
Resolve merge conflicts
- Run
git statusto see conflicted files. - Open each file and edit the sections between
<<<<<<<,=======and>>>>>>>. git add fileand thengit commit(orgit rebase --continue).
Full walkthrough: how to resolve Git merge conflicts.
Ignore files
# .gitignore
node_modules/
.env
dist/
Golden rules
- Commit small, with clear messages.
- Never rewrite history (
--amend,rebase,reset --hardthen force push) on branches others use. - Pull before you push.
- Never commit secrets.
Learn more
Start with what Git is and Git basics.
