Git Cheatsheet#
Keep this open while you practise. Each group links to the session that teaches it.
The picture to remember#
Set up (Session 1, Setup)#
Command |
What it does |
|---|---|
|
your name on commits |
|
your email on commits |
|
new repos start on |
|
show your settings |
|
manual / short help |
Start a repository (Session 2)#
Command |
What it does |
|---|---|
|
make the current folder a repository |
|
download a repository |
|
what is changed? (long / short) |
|
stage a file / everything / piece by piece |
|
save the staged changes |
|
stage modified tracked files and commit |
Look around (Session 3)#
Command |
What it does |
|---|---|
|
one line per commit |
|
draw all branches |
|
with full changes / file summary |
|
filter history |
|
one commit in detail |
|
unstaged changes |
|
what the next commit will contain |
|
all changes since last commit |
|
between two commits |
|
who changed each line |
|
files Git tracks |
HEAD = current commit · HEAD~1 = one before · HEAD~3 = three before.
Ignore files (Session 3)#
.env # one file
*.log # by pattern
logs/ # a folder
!keep.log # exception
Already tracked? git rm --cached <file>.
Undo (Session 4)#
Situation |
Command |
|---|---|
Discard edits in a file |
|
Un-stage a file |
|
Fix the last commit (message / forgotten file) |
|
Undo commit, keep changes staged |
|
Undo commit, keep changes unstaged |
|
Undo commit and discard changes |
|
Safely undo a pushed commit |
|
Find lost commits |
|
Delete untracked files (dry run first) |
|
Remove / rename a tracked file |
|
Branches (Session 5)#
Command |
What it does |
|---|---|
|
list local / all branches |
|
go to a branch |
|
create and go to a branch |
|
go back to the previous branch |
|
rename |
|
delete (safe / force) |
|
branches already merged (safe to delete) |
Merge and conflicts (Session 6)#
Command |
What it does |
|---|---|
|
bring |
|
always create a merge commit |
|
stage all its changes as one; you commit |
|
cancel a merge in progress |
Resolve a conflict: edit the file → remove <<<<<<<, =======, >>>>>>> → git add <file> → git commit.
Remotes (Session 7)#
Command |
What it does |
|---|---|
|
list remotes |
|
link a remote |
|
change its URL |
|
first push; sets upstream |
|
share / catch up (pull = fetch + merge) |
|
download only, change nothing |
|
delete a remote branch |
|
forget deleted remote branches |
|
the safer force-push (own branches only) |
Pull requests and forks (Session 8)#
Step |
Command / place |
|---|---|
Branch |
|
Publish |
|
Open PR |
GitHub → Compare & pull request |
Update PR |
commit + |
Clean up |
|
Fork setup |
|
Sync fork |
|
Power tools (Session 9)#
Command |
What it does |
|---|---|
|
shelve changes (and untracked files) |
|
manage the shelf |
|
replay my branch on top of |
|
tidy the last 3 commits |
|
during a rebase conflict |
|
copy one commit here |
|
annotated tag on this commit |
|
publish tags |
Warning
Rebase, amend, reset and force-push rewrite history. Use them only on commits nobody else has.
Workflows at a glance (Sessions 10 and 11)#
Workflow |
Remember it as |
Main branches |
|---|---|---|
Centralized |
everyone on |
|
Feature Branch |
a branch per task |
|
GitHub Flow |
feature branch + PR + deploy |
|
Git Flow |
versioned releases |
|
Forking |
PRs from your own copy |
upstream + your fork |
Trunk-based |
tiny commits, feature flags |
|
Rescue (Session 12)#
Problem |
First aid |
|---|---|
Lost? |
|
Committed on the wrong branch |
|
Detached HEAD, want to keep work |
|
Bad merge, not pushed |
|
Bad merge, pushed |
|
Pushed a secret |
rotate the secret first, then scrub history |
Which commit broke it? |
|
Backup before anything risky |
|
Good commit message#
Fix wrong total when a coupon is applied ← imperative, ≈ 50 characters
Why it was wrong and how this fixes it. Fixes #87.