Git basics

Repo & config

GoalCommand
New repogit init
Clonegit clone <url>
Clone shallowgit clone --depth 1 <url>
Identitygit config --global user.name "...", git config --global user.email "..."
Show settinggit config --get user.email

Status & diff

GoalCommand
Overviewgit status
Shortgit status -sb
Unstaged changesgit diff
Staged vs last commitgit diff --staged (same as --cached)
File historygit log --follow -p -- <path>
One commitgit show <commit>

Stage & commit

GoalCommand
Stage pathsgit add <path>
Stage all (tracked + new)git add -A
Stage interactivelygit add -p
Commitgit commit -m "message"
Stage tracked + commitgit commit -am "message"
Amend last commit (keep msg)git commit --amend --no-edit
Amend with new messagegit commit --amend -m "message"
Change last commit’s datesGIT_AUTHOR_DATE="2024-01-15T10:00:00" GIT_COMMITTER_DATE="2024-01-15T10:00:00" git commit --amend --no-edit — or author only: git commit --amend --no-edit --date="2024-01-15T10:00:00" (committer stays current time)
Change an older commit’s dategit rebase -i <commit>^edit / e on that line → set GIT_AUTHOR_DATE / GIT_COMMITTER_DATE as above → git commit --amend --no-editgit rebase --continue

Log & inspect

GoalCommand
Log compactgit log --oneline --graph --decorate -20
Log with statsgit log --stat
Who changed a linegit blame <path>
Branches containing commitgit branch -a --contains <commit>

Branch, switch & checkout

GoalCommand
List localgit branch
List allgit branch -a
Create (stay on current)git branch <name>
Switch branchgit switch <name> or git checkout <name>
Create branch + switch to itgit switch -c <name> or git checkout -b <name>
Detached HEAD at a commitgit checkout <commit> or git switch --detach <commit>
Checkout tracks remote branchgit checkout <name> (if origin/<name> exists and name is unique) — or git switch -c <name> --track origin/<name>
Delete mergedgit branch -d <name>
Delete forcegit branch -D <name>
Rename currentgit branch -m <new-name>

git checkout can also take paths after --: it does not switch branches then; it copies those files into your working tree from somewhere else—usually the index (git checkout -- <path>) or a specific revision (git checkout HEAD -- <path>, git checkout <branch> -- <path>). That is how people used to discard edits or pull one file from another branch. Prefer git restore for that today (git restore <path>, git restore --source=HEAD <path>). For moving HEAD to a branch, git switch (Git 2.23+) is the dedicated command. Older docs and scripts still use checkout for everything.

Remote & sync

GoalCommand
List remotesgit remote -v
Add remotegit remote add origin <url>
Fetchgit fetch
Fetch prune deleted branchesgit fetch --prune
Pullgit pull
Pull rebasegit pull --rebase
Push branchgit push -u origin <branch>
Push currentgit push
Force push (overwrite remote)git push --force / git push -f — rewrites remote history; avoid on shared branches unless your team expects it
Safer force pushgit push --force-with-lease — refuses if the remote tip changed since your last fetch (still history rewrite; use with care)
Delete remote branchgit push origin --delete <branch>

Merge & rebase

GoalCommand
Merge branch into currentgit merge <branch>
Abort mergegit merge --abort
Rebase ontogit rebase <upstream-branch>
Interactive rebase last N commitsgit rebase -i HEAD~N
Interactive rebase from a specific commit (and after)git rebase -i <commit>^ — same as git rebase -i <commit>~1 on linear history: both mean “rebase onto the parent of <commit>”, so the todo lists <commit> and all commits after it; use git rebase -i --root if <commit> is the first commit
Reword that commit’s messageIn the todo file: reword / r instead of pick on that line → save → edit message in the next editor step
Edit that commit’s snapshotIn the todo file: edit / e instead of pick on that line → save → change files → git add …git commit --amendgit rebase --continue
Abort rebasegit rebase --abort
Continue after conflict or amendgit rebase --continue
Cherry-pickgit cherry-pick <commit>

Undo & clean up

GoalCommand
Unstage filegit restore --staged <path>
Discard working-tree filegit restore <path>
Soft reset (keep changes staged)git reset --soft HEAD~1
Mixed reset (default, unstage)git reset HEAD~1
Hard reset (destructive)git reset --hard <commit>
Revert a commit (new commit)git revert <commit>
Remove untracked files/dirsgit clean -fd (dry-run: git clean -fdn)

Stash

GoalCommand
Stashgit stash push -m "note"
Listgit stash list
Apply latestgit stash apply
Apply + dropgit stash pop
Drop latestgit stash drop

Tags

GoalCommand
Listgit tag
Annotated taggit tag -a v1.0 -m "release"
Push tagsgit push --tags

Misc

GoalCommand
Recover moved HEAD / lost commitsgit reflog
Ignore patterns file.gitignore in repo root

See also docker.