git Distributed version control system, created by Linus Torvalds

Command Reference

A quick-lookup cheat sheet. See the linked page for each section for full explanations and examples.

Setup

CommandDescription
git config --global user.name "..."Set your commit author name
git config --global user.email "..."Set your commit author email
git initCreate a new, empty repository
git clone <url>Copy an existing repository

Basic Workflow

CommandDescription
git statusShow staged / unstaged / untracked files
git add <path>Stage a file or directory
git add -pInteractively stage hunks
git commit -m "..."Record a snapshot
git commit --amendEdit the most recent commit
git diffWorking dir vs. staging area
git diff --stagedStaging area vs. last commit
git log --oneline --graph --allCompact history graph
git show <sha>Full details of one commit
git rm / git mvRemove / rename a tracked file

Branching & Merging

CommandDescription
git branchList local branches
git switch -c <name>Create and switch to a branch
git branch -d <name>Delete a merged branch
git merge <branch>Merge a branch into the current one
git merge --abortCancel a merge with conflicts

Remotes

CommandDescription
git remote add origin <url>Add a remote
git fetchDownload commits, don't merge
git pullFetch + merge current branch's upstream
git push -u origin <branch>Push and set upstream tracking
git push --force-with-leaseSafe force-push after history rewrite

Undoing Changes

CommandDescription
git restore <file>Discard unstaged changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard <commit>Discard everything after a commit
git revert <sha>Undo a commit with a new, safe commit
git reflogRecover a "lost" commit

Stashing

CommandDescription
git stashShelve uncommitted changes
git stash popReapply and remove the latest stash
git stash listShow the stash stack

Tags

CommandDescription
git tag -a v1.0.0 -m "..."Create an annotated tag
git push origin --tagsPush every local tag

Rebasing & Cherry-Picking

CommandDescription
git rebase mainReplay current branch's commits on top of main
git rebase -i HEAD~5Interactively edit/squash the last 5 commits
git rebase --continue / --abortResume or cancel a paused rebase
git cherry-pick <sha>Apply one commit onto the current branch

Inspecting & searching

CommandDescription
git blame <file>Who last changed each line, and in which commit
git log -S"text"Find commits that added/removed a string (pickaxe search)
git bisect startBinary-search history for the commit that introduced a bug
git grep "pattern"Search tracked files at the current checkout