Git#
Common Git operations as a quick-lookup reference. For background, see Git.
Status and Log#
The read-only side of git, “what is here” and “what happened.”
git status for the working tree, git log for history,
git diff for changes. Most session-time spent in git is in
this category.
Command |
Action |
|---|---|
|
working-tree status |
|
short with branch info |
|
branchy log |
|
with diffs |
|
by time |
|
by author |
|
commits adding/removing the string |
|
history through renames |
|
line-by-line authorship |
|
show a commit |
|
unstaged changes |
|
staged |
|
Staging and Committing#
The write side: choose what goes into the next commit, then make
it. git add -p is the operator’s friend for splitting a
working tree into clean, reviewable commits; --amend is fine
on local commits and dangerous on pushed ones.
Command |
Action |
|---|---|
|
stage path |
|
stage hunks interactively |
|
|
|
discard unstaged changes |
|
commit |
|
amend last (only on unpushed) |
|
|
|
|
|
sign-off |
|
GPG-sign |
Branches#
Create, list, switch, and delete. git switch is the modern
verb (replaces overloaded git checkout); - returns to the
previous branch the same way cd - does in the shell.
Command |
Action |
|---|---|
|
list local |
|
list all (incl. remote) |
|
create + switch |
|
switch |
|
switch to previous |
|
delete (merged) |
|
delete (force) |
|
rename |
|
|
|
Remote#
Move commits between your repo and the network. --prune keeps
the local view of remote branches honest; --force-with-lease
is the safe-ish force-push (refuses if someone else’s commits
arrived after you last fetched).
Command |
Action |
|---|---|
|
list remotes |
|
fetch from origin |
|
fetch all, remove deleted refs |
|
rebase pull |
|
push |
|
set upstream + push |
|
safer force-push |
|
push tags |
|
Merging and Rebasing#
The two ways to combine branch histories. Merge preserves the
structure of what happened; rebase rewrites it for a linear history.
Interactive rebase (-i) is the workhorse for cleaning up
local commits before pushing.
Command |
Action |
|---|---|
|
merge |
|
|
|
|
|
rebase onto main |
|
interactive (squash, reword, drop) |
|
use |
|
after resolving |
|
stop |
|
apply a commit elsewhere |
Stashing#
A drawer for in-progress work. git stash parks dirty changes
on a stack so you can pull, switch branches, or test something
clean, then git stash pop brings them back. -u includes
untracked files, which the default omits.
Command |
Action |
|---|---|
|
stash dirty tree |
|
include untracked |
|
list |
|
show diff of stash N |
|
apply + drop top |
|
apply stash N (keep) |
|
delete stash |
|
new branch from stash |
Undoing#
The recovery toolkit. revert is safe (a new commit reversing
the old); reset --soft rewinds the branch but keeps your
work; reset --hard deletes work and is the operation people
regret. git reflog is the lifeline when something has gone
sideways.
Command |
Action |
|---|---|
|
discard unstaged |
|
|
|
new commit reversing SHA |
|
undo last commit, keep changes staged |
|
|
|
undo, discard (careful) |
|
recent HEAD movements |
|
Searching#
Find code, find commits, find regressions. git grep is faster
than grep over the working tree because it knows what’s
tracked; git log -G searches across the diff history;
git bisect is the binary-search tool for “this used to work.”
Command |
Action |
|---|---|
|
search tracked files |
|
with line numbers |
|
commits with matching diff |
|
|
|
|
|
finish |
Submodules and Subtrees#
Two ways to nest one git repo inside another. Submodules keep the nested repo as its own entity (separate history, harder to clone); subtrees vendor the nested code into the parent’s history (easier to clone, awkward to push back upstream).
Command |
Action |
|---|---|
|
|
|
|
|
|
|
Worktrees#
Multiple working trees backed by the same repo, each on a different branch. The operator pattern: leave the main checkout alone, run a hotfix in a worktree, leave a long-running rebase in another, no need to clone or stash to switch contexts.
Command |
Action |
|---|---|
|
|
|
list worktrees |
|
remove |