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

git status

working-tree status

git status -sb

short with branch info

git log --oneline --graph --all --decorate

branchy log

git log -p

with diffs

git log --since='1 week'

by time

git log --author=operator

by author

git log -S 'regex'

commits adding/removing the string

git log --follow path

history through renames

git blame path

line-by-line authorship

git show <sha>

show a commit

git diff

unstaged changes

git diff --staged

staged

git diff branch1..branch2 between branches

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

git add path

stage path

git add -p

stage hunks interactively

git restore --staged path unstage

git restore path

discard unstaged changes

git commit -m "msg"

commit

git commit --amend

amend last (only on unpushed)

git commit --amend --no-edit amend without editing message

git commit --fixup=<sha> fixup commit; squash later

git commit -s

sign-off

git commit -S

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

git branch

list local

git branch -a

list all (incl. remote)

git switch -c name

create + switch

git switch name

switch

git switch -

switch to previous

git branch -d name

delete (merged)

git branch -D name

delete (force)

git branch -m old new

rename

git branch --merged main branches merged into main

git branch --no-merged main not merged

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

git remote -v

list remotes

git fetch

fetch from origin

git fetch --all --prune

fetch all, remove deleted refs

git pull --rebase

rebase pull

git push

push

git push -u origin BRANCH

set upstream + push

git push --force-with-lease

safer force-push

git push --tags

push tags

git push origin --delete BRANCH delete remote branch

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

git merge BRANCH

merge

git merge --no-ff BRANCH keep merge commit

git merge --squash BRANCH collect into one commit

git rebase main

rebase onto main

git rebase -i main

interactive (squash, reword, drop)

git rebase --autosquash

use --fixup markers

git rebase --continue

after resolving

git rebase --abort

stop

git cherry-pick SHA

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

git stash

stash dirty tree

git stash -u

include untracked

git stash list

list

git stash show -p N

show diff of stash N

git stash pop

apply + drop top

git stash apply N

apply stash N (keep)

git stash drop N

delete stash

git stash branch NAME

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

git restore path

discard unstaged

git restore --staged path unstage

git revert SHA

new commit reversing SHA

git reset --soft HEAD~1

undo last commit, keep changes staged

git reset --mixed HEAD~1 undo, unstage

git reset --hard HEAD~1

undo, discard (careful)

git reflog

recent HEAD movements

git reset --hard HEAD@{2} go back to a reflog state

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

git grep PATTERN

search tracked files

git grep -n PATTERN

with line numbers

git log -G regex

commits with matching diff

git bisect start good bad binary search for regression

git bisect good / bad mark current commit

git bisect reset

finish

Tags#

Mark a specific commit with a name, usually a release. Annotated tags (-a) carry a message and an author, which is what release processes want; lightweight tags are just a name pointing at a commit.

Command

Action

git tag

list

git tag -a v1.0 -m "msg" annotated tag

git tag -d v1.0

delete tag locally

git push origin v1.0

push tag

git push origin --delete v1.0 delete remote tag

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

git submodule add URL DIR add submodule

git submodule update --init --recursive clone subs

git submodule foreach 'git pull' update all

git subtree add --prefix=DIR URL BRANCH add subtree

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

git worktree add ../wt BRANCH add a working tree

git worktree list

list worktrees

git worktree remove DIR

remove