Git cheatsheet
Setup and clone
| Command | Description |
|---|---|
git config --global user.name "Name" | Set commit author name |
git config --global user.email "a@b.com" | Set commit author email |
git clone git@host:org/repo.git | Clone via SSH |
git clone --depth 1 url | Shallow clone (CI) |
Daily workflow
| Command | Description |
|---|---|
git status | Changed and staged files |
git add filegit add -p | Stage (patch = hunk by hunk) |
git commit -m "msg" | Create commit |
git pull --rebase | Fetch and rebase onto remote |
git push origin branch | Push branch to remote |
git log --oneline -10 | Recent commits, one line each |
git diffgit diff --staged | Unstaged vs staged diff |
Branches
| Command | Description |
|---|---|
git branch | List local branches |
git checkout -b feature/x | Create and switch branch |
git switch main | Switch branch (Git 2.23+) |
git merge feature/x | Merge branch into current |
git branch -d feature/x | Delete merged branch |
Remotes
git remote -v
git fetch origin
git push -u origin main # set upstream tracking
git remote add upstream URL
git fetch upstream && git merge upstream/main
Undo and fix mistakes
| Command | Description |
|---|---|
git restore file | Discard working tree changes |
git restore --staged file | Unstage file |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --hard HEAD~1 | Discard last commit and changes |
git revert SHA | New commit that undoes a commit (safe on shared branches) |
git stashgit stash pop | Temporarily shelve changes |
Inspect history
git show SHA
git blame file
git log --graph --oneline --all
git reflog # recover "lost" commits
Tags
git tag v1.0.0
git tag -a v1.0.0 -m "Release"
git push origin v1.0.0
git checkout tags/v1.0.0
Pro tips
git statusbefore every commit — know what you are recording- Pull before push on shared branches to avoid non-fast-forward rejections
- Use
git reverton shared history; reservereset --hardfor local-only fixes git reflogrecovers commits after a mistaken reset- Large binaries belong in Git LFS or artifact storage, not plain Git
Practice scenarios
Hands-on Git scenarios on live Linux VMs: git