Git Commands Cheat Sheet
Quick reference for the most useful Git commands: init, clone, branch, merge, rebase, reset, stash, remote operations and history inspection, with common options and practical examples.
31 commands
Getting Started
git initCreate a new Git repository in the current directory.
git init my-projectgit clone <url>Copy a remote repository to your local machine, including full history.
--depth 1 shallow clone (latest commit only); -b <branch> clone a specific branch
git clone -b main --depth 1 https://github.com/user/repo.gitgit configSet user name, email and other options; --global applies to all repositories.
git config --global user.name "Your Name" && git config --global user.email "[email protected]"Everyday Work
git statusShow working tree state: modified, staged and untracked files.
-s short format; -b show branch info
git status -sbgit add <file>Stage file changes for the next commit.
-A stage all changes; -p interactively pick hunks; -u stage tracked files only
git add -p src/index.tsgit commitRecord staged changes with a message.
-m message inline; -a stage tracked files and commit; --amend rewrite the last commit
git commit -am "fix: handle empty input"git diffShow line-level changes between working tree, index and commits.
--staged staged vs last commit; <branch> compare with a branch; --stat summary only
git diff --staged --statgit restore <file>Discard uncommitted changes in the working tree (or unstage with --staged).
git restore --staged src/index.tsBranching
git branchList, create or delete branches.
-d delete; -D force delete; -m rename; -a list remote branches too
git branch -m old-name new-namegit switch <branch>Switch to another branch; -c creates it first (modern replacement for checkout).
git switch -c feature/logingit merge <branch>Merge another branch into the current one.
--no-ff always create a merge commit; --abort cancel a conflicted merge
git merge --no-ff feature/logingit rebase <branch>Replay your commits on top of another branch for a linear history.
-i interactive (squash/reword/reorder); --abort cancel; --continue after resolving
git rebase -i HEAD~3Remote
git remote -vList configured remote repositories with URLs.
git remote add origin https://github.com/user/repo.gitgit fetchDownload objects and refs from the remote without merging.
--prune delete stale remote-tracking branches; --all fetch all remotes
git fetch --prune origingit pullFetch from the remote and merge (or rebase) into the current branch.
--rebase rebase instead of merge; --ff-only fast-forward only
git pull --rebase origin maingit pushUpload local commits to the remote repository.
-u set upstream tracking; --force-with-lease safer force push; --tags push tags
git push -u origin feature/loginUndo & Fix
git resetMove HEAD to another commit, optionally changing index and working tree.
--soft keep changes staged; --mixed (default) keep changes unstaged; --hard discard everything
git reset --hard HEAD~1git revert <commit>Create a new commit that undoes a previous commit. Safe for shared history.
git revert HEADgit commit --amendRewrite the most recent commit: fix its message or add forgotten changes.
--no-edit keep the original message
git commit --amend --no-editgit cleanRemove untracked files from the working tree.
-n dry run; -f force; -d include directories; -x include ignored files
git clean -ndxHistory & Inspect
git logShow commit history.
--oneline one line per commit; --graph branch graph; -p show diffs; -n limit count
git log --oneline --graph -15git show <commit>Show a commit's message and diff; also works for tags and files (git show HEAD:file).
git show HEAD~2 --statgit blame <file>Show who last modified each line of a file and in which commit.
-L n,m restrict to a line range
git blame -L 10,20 src/index.tsgit reflogShow the history of HEAD movements — recover lost commits after a bad reset or rebase.
git reflog -20Stash
git stashTemporarily shelve uncommitted changes and clean the working tree.
-u include untracked files; push -m message; list show all stashes
git stash push -u -m "wip: login form"git stash popRe-apply the most recent stash and remove it from the stash list.
apply re-apply but keep the stash entry
git stash apply stash@{1}Tags & Release
git tagList, create or delete tags marking release points.
-a annotated tag; -m message; -d delete
git tag -a v1.2.0 -m "Release 1.2.0"git describe --tagsShow the most recent tag reachable from the current commit, plus distance.
git describe --tags --alwaysAdvanced
git cherry-pick <commit>Apply the changes of a specific commit onto the current branch.
-n apply without committing; -x append source commit hash to message
git cherry-pick -x a1b2c3dgit bisectBinary-search the history to find the commit that introduced a bug.
start / bad / good / run <cmd> / reset
git bisect start && git bisect bad && git bisect good v1.0.0git worktree add <path> <branch>Check out another branch into a separate directory sharing the same repository.
git worktree add ../hotfix hotfix/urgent-bugCheatsheet version 1.0.0
Covers Git 2.30+