git
Git 2.30+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-bugRelated command cheatsheets
Trademark & Use Notice
These command-line tool cheatsheets are open community references. The names, logos, and trademarks of the tools referenced here (such as Git™, Docker™, Kubernetes®, kubectl, PostgreSQL®, MySQL®, Redis™, MongoDB®, Linux™, PowerShell™, Vim™, and others) are the property of their respective owners and are used here solely for identification and reference purposes.
All content in these cheatsheets is independently authored as a simplified, self-contained reference. It is not affiliated with, endorsed by, or sourced from any official documentation. Command listings, flag descriptions, and examples are original simplifications; for authoritative information please consult the official documentation of each tool.
Command syntax and option flags may differ across tool versions. The version tags shown reflect stable releases; behavior may differ in other versions. This cheatsheet does not imply any preference or recommendation by the trademark holders.
If a trademark or copyright owner believes that any content on this site infringes their rights, please send an email to [email protected]. Upon receiving valid proof of rights, we will modify or remove the relevant content within a reasonable period.
This website assumes no legal liability for any direct or indirect losses arising from the use of information presented on these pages.
About Git
Git is a free, open-source distributed version control system created by Linus Torvalds in April 2005 to manage the Linux kernel's source tree after the BitKeeper license dispute. Every working directory is a full repository with its own history, so commits, branches, and merges are local until you push; this is what makes Git fast and lets thousands of developers work offline. The current stable line is Git 2.40+. Git's content-addressable filesystem stores data as SHA-1 (now moving to SHA-256) objects — `commit`, `tree`, `blob`, `tag` — and supports staging with the index, cheap branching, rebasing, bisect for bug hunting, submodules, worktrees, and signed commits with GPG/SSH. It is the de facto standard for source-code management, used by both open-source projects and enterprises. Use it whenever you want history, branching, or collaboration; reach for `git rebase -i` to clean up before review, and `git reflog` to recover 'lost' work. Git never uploads secrets you did not commit, but it does remember anything you did commit — scrub sensitive data with `git filter-repo` before pushing to a public host.
Cheatsheet version 1.0.0