Git Commands Reference
Every Git command that matters, one page per command. Common flags, real examples, and the
footguns that trip people up. Modern Git (2.30+) — including the newer switch
and restore that replaced overloaded checkout.
Basics
git clone
Downloads a repository from a remote URL into a new local directory, setting up the remote 'origin' automatically. Includes all commits, branches, and tags by default.
git init
Creates a new empty Git repository by initializing the .git metadata directory in the current or specified folder. Turns any directory into a version-controlled project.
git add
Stages file changes into the index so they are included in the next commit. Supports whole files, directories, and interactive patch selection.
git commit
Records the staged snapshot to the repository history along with a message and author metadata. Every commit is identified by a unique SHA hash.
git config
Reads and writes Git configuration variables at system, global, or repository scope. Controls identity, aliases, editor, merge tools, and much more.
History & Diff
git log
Displays commit history in reverse chronological order with filters for author, date, path, or content. The primary tool for exploring project history.
git cherry-pick
Applies the changes from one or more specific commits onto the current branch as new commits. Useful for porting bug fixes across branches.
git tag
Marks specific commits with a permanent name, typically for release versions like v1.2.0. Supports lightweight tags (a ref) or annotated tags (a full object with metadata).
Branching
git branch
Lists, creates, renames, or deletes branches — pointers to commits. Does not switch branches on its own; use switch or checkout for that.
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git switch
Modern, focused command (Git 2.23+) that only switches branches — no file restoration overloading like checkout. Safer and clearer for everyday branch changes.
git merge
Joins the history of another branch into the current one, creating a merge commit when histories diverge. Preserves the full branching topology.
git rebase
Replays commits from your branch on top of another base, producing a linear history without merge commits. Powerful for cleaning up history before pushing.
Remote & Collaboration
Sync (Push / Pull / Fetch)
git push
Uploads local commits from a branch to a remote repository, updating the remote-tracking references. Requires push permission on the remote.
git pull
Fetches from the remote and integrates the changes into the current branch, effectively 'git fetch' followed by 'git merge' (or rebase). Updates your working tree with upstream commits.
git fetch
Downloads objects and refs from a remote without modifying your working tree or current branch. Safest way to see what's new upstream before integrating.
Inspection
git status
Shows the state of the working tree and index: staged, unstaged, and untracked files, plus branch tracking info. The first command to run when you're unsure what changed.
git diff
Shows line-level differences between commits, the index, and the working tree. Without arguments, compares unstaged changes to the index.
Undo & Recovery
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git reset
Moves the current branch tip to a specified commit, optionally updating the index and working tree. Used to unstage files or rewind local history.
git revert
Creates a new commit that inverts the changes of a previous commit, preserving history. The safe way to undo pushed commits on shared branches.
git stash
Shelves uncommitted changes onto a stack so you can switch context with a clean working tree. Later, reapply them with pop or apply.
git reflog
Shows the log of updates to branch tips and HEAD stored locally, even after commits are dropped from history. The rescue rope for recovering 'lost' commits.