01
The four areas
where your work livesEvery command moves work between these zones. Knowing the arrows is half of Git.
where you edit
what the next commit will contain
safe, committed snapshots
shared / backup
02
One-time setup
~/.gitconfig
git config --global user.name "Name"
Identity stamped on every commit you make. ★
git config --global user.email "you@x.com"
Email attached to your commits and tags. ★
git config --global color.ui auto
Colorize Git's output for easier reading.
git config --global core.editor nano
Editor used for commit messages & interactive rebase.
git config --global alias.lg "log --oneline"
Make a shortcut — now
git lg works.
git config --list --show-origin
List all settings and which file each came from.
03
Start a project
init / clone
git init [dir]
Turn a folder into a new, empty repository. ★
git clone <url>
Download a repo with its full history from a remote. ★
git clone <url> [dir]
Clone into a specific local directory name.
04
Stage & commit
the daily loop
git status
What's staged, unstaged, and untracked. Run it constantly. ★
git add <file>
Stage a file as it looks now for the next commit. ★
git add .
Stage every change in the current tree. ★
git add -p
Pick which hunks of a file to stage, interactively.
git commit -m "message"
Snapshot the staged content with a message. ★
git commit -am "message"
Stage all tracked changes and commit in one step.
git diff
Changes in the working dir that aren't staged yet.
git diff --staged
Changes staged but not yet committed.
git rm <file>
Delete a file and stage the removal.
git rm --cached <file>
Stop tracking a file, but keep it on disk.
git mv <old> <new>
Rename / move a file and stage the change.
05
File lifecycle
untracked → committedA file's journey through Git's states — and the commands that move it.
flowchart LR
U([Untracked]):::u -->|git add| S[Staged]:::s
W([Modified]):::w -->|git add| S
S -->|git commit| C[(Committed)]:::c
C -->|edit file| W
S -->|git reset| W
W -->|git restore| C
classDef u fill:#1c2230,stroke:#f85149,color:#f85149;
classDef w fill:#1c2230,stroke:#d29922,color:#d29922;
classDef s fill:#1c2230,stroke:#f0883e,color:#f0883e;
classDef c fill:#1c2230,stroke:#3fb950,color:#3fb950;
06
Branch & merge
isolate & integrate
git branch
List branches;
* marks the current one. ★
git branch -a
Include remote-tracking branches in the list.
git switch -c <name>
Create a branch and switch to it (modern form). ★
git checkout -b <name>
Same thing, classic form — create & check out.
git switch <name>
Move onto an existing branch (or
checkout). ★
git merge <branch>
Pull another branch's history into the current one. ★
git branch -d <name>
Delete a branch that's already merged.
git branch -D <name>
Force-delete even if unmerged. loses work
07
Merge vs rebase
two ways to combineMerge ties histories together with a merge commit. Rebase replays your commits on top for a straight line.
git merge <branch>
Non-destructive; preserves the true branch shape. ★
git merge --squash <branch>
Collapse a branch into one set of staged changes.
git rebase <base>
Replay commits onto base for linear history. rewrites
git cherry-pick <commit>
Copy a single commit onto the current branch.
08
Remotes & sync
share & update
git remote add <name> <url>
Register a remote URL under a short alias (e.g.
origin).
git pull
Fetch and merge the tracking branch in one move. ★
git pull --rebase
Fetch, then rebase your work on top — cleaner line.
git push
Send the current branch's commits to its remote. ★
git push -u origin <branch>
First push of a new branch; sets its upstream. ★
git fetch [remote]
Download remote commits without touching your branches.
git push --tags
Tags aren't pushed by default — send them explicitly.
git push --force-with-lease
Overwrite remote history, but abort if others pushed. careful
09
Inspect history
log · diff · blame
git log --oneline --graph --decorate
Compact, visual history with branch labels. ★
git log -<n>
Limit to the last
n commits.
git log branchA..branchB
Commits on B that aren't on A.
git log --follow <file>
History of a file, even across renames.
git log --author="name"
Filter commits by author (also
--grep, --after).
git show <commit>
Full diff & metadata for one object.
git diff <commit> <commit>
Compare any two points in history.
git blame <file>
Who last changed each line of a file.
git reflog
Log of where HEAD has been — your safety net.
10
Undo & recover
restore · reset · revert
git restore <file>
Throw away unstaged edits to a file. ★
git reset <file>
Unstage a file but keep its changes. ★
git restore <file> --source <commit>
Bring back a file as it was in an older commit.
git revert <commit>
New commit that undoes a past one — safe on shared branches. ★
git reset HEAD^
Undo the last commit, keep changes in working dir.
git reset --hard <commit>
Move branch & wipe all changes after it. irreversible
git clean -f
Delete untracked files (
-n previews first). deletes11
Stash
shelve work
git stash
Shelve tracked changes & clean the working dir. ★
git stash pop
Re-apply the latest stash and drop it. ★
git stash list
Show the stack of stashed changes.
git stash drop
Discard the most recent stash entry.
12
Tags
mark releases
git tag
List all tags.
git tag -a <name> -m "msg"
Annotated tag (a real object) for the current commit. ★
git tag <name> <sha>
Lightweight tag pointing at a specific commit.
git tag -d <name>
Remove a tag locally.
13
Rewrite history
local branches only
git commit --amend
Fold staged changes into the last commit / fix its message. ★
git rebase -i HEAD~<n>
Reorder, squash or edit the last n commits. rewrites
git reflog <branch>
Find a lost commit ID to recover after a bad rebase.
14
Refs & ignoring
how to name a commitAnywhere a command wants <commit>, any of these work:
main · origin/main
A branch / remote-tracking branch.
v0.1
A tag name.
3e887ab
A commit SHA (first 7 chars suffice).
HEAD
The commit you're currently on.
HEAD~3 · HEAD^^^
Three commits back from HEAD.
.gitignore — keep noise out of the repo:
# build artefacts & logs
logs/
*.swp
/tmp
!logs/.gitkeep # un-ignore
Rules apply to the folder containing the file and all children.