Alex Garbiak
← Lib

Git Command Reference

Updated gitclicheatsheet

A general-purpose baseline for now — the plan is to trim this down to the commands and flags I actually use day to day, and drop the rest.

Setup & config

CommandDescription
git initCreate a new repository here.
git clone <url>Clone a remote repository.
git config --global user.name "<name>"Set your commit author name.
git config --global user.email "<email>"Set your commit author email.
git config --listShow effective config.

Everyday snapshot

git status

Show the state of the working tree: staged, unstaged, and untracked changes.

FlagDescription
-s, --shortCompact one-line-per-file output.
-bShow branch and tracking info (used with -s).

git add

Stage changes for the next commit.

FlagDescription
-A, --allStage everything (new, modified, deleted).
-p, --patchInteractively choose hunks to stage.
-uStage modifications and deletions only, not new files.

git commit

Record staged changes to the repository.

FlagDescription
-m "<msg>"Commit message inline.
-aAuto-stage tracked, modified files before committing.
--amendRewrite the previous commit instead of creating a new one.
--no-verifySkip pre-commit/commit-msg hooks.

git diff

Show changes between commits, the working tree, and the index.

FlagDescription
--staged, --cachedDiff what’s staged against the last commit.
<file>Limit the diff to one file or path.
<branch>..<branch>Diff between two branches.

Branching & merging

CommandDescription
git branchList local branches.
git branch <name>Create a new branch.
git branch -d <name>Delete a branch (safe — refuses if unmerged).
git branch -D <name>Force-delete a branch.
git switch <branch>Switch to an existing branch.
git switch -c <branch>Create and switch to a new branch.
git checkout <branch>Older equivalent of switch (also used for restoring files).
git merge <branch>Merge <branch> into the current branch.
git rebase <branch>Replay the current branch’s commits on top of <branch>.

Inspecting history

git log

Show commit history.

FlagDescription
--onelineOne line per commit.
--graphDraw an ASCII branch/merge graph.
-pShow the full diff for each commit.
-n <count>Limit to the last <count> commits.
--author="<name>"Filter by author.

git show

Show the details and diff of a single commit, tag, or object.

Working with remotes

CommandDescription
git remote -vList remotes and their URLs.
git remote add <name> <url>Add a new remote.
git fetch <remote>Download objects/refs without merging.
git pullFetch and merge (or rebase, with --rebase) into the current branch.
git pushPush the current branch to its upstream.
git push -u <remote> <branch>Push and set the upstream tracking branch.
git push --force-with-leaseForce-push safely — fails if the remote has commits you haven’t seen.

Undoing things

CommandDescription
git restore <file>Discard unstaged changes to a file.
git restore --staged <file>Unstage a file, keeping the changes.
git reset --soft <commit>Move HEAD, keep changes staged.
git reset --mixed <commit>Move HEAD, keep changes unstaged (default mode).
git reset --hard <commit>Move HEAD and discard all changes — destructive.
git revert <commit>Create a new commit that undoes <commit>.

Stashing

CommandDescription
git stashShelve uncommitted changes.
git stash -uAlso stash untracked files.
git stash listList stashes.
git stash popReapply the most recent stash and drop it.
git stash applyReapply the most recent stash but keep it in the list.

Tags

CommandDescription
git tagList tags.
git tag <name>Create a lightweight tag.
git tag -a <name> -m "<msg>"Create an annotated tag.
git push <remote> <tag>Push a single tag.
git push <remote> --tagsPush all tags.