Git cheat sheet

Searchable list of the most useful Git commands for setup, branches, remotes, undoing changes and inspecting history.

Runs in your browser ยท nothing is sent

55 commands

git initCreate a new, empty repository in the current folder.

Creates a hidden .git folder that stores the whole history. Run it once per project, then add files and make a first commit.

Example

git init -b main

Options

-b <name>Name of the first branch, for example main.
--bareRepository without working files, used on servers as a central remote.
git clone <url> [folder]Copy a remote repository, including its full history.

Downloads all commits and branches, checks out the default branch and sets the remote called origin.

Example

git clone https://github.com/user/repo.git my-folder

Options

--depth 1Shallow clone with only the latest commit. Much faster for big repositories and CI.
-b <branch>Check out this branch instead of the default one.
--recurse-submodulesAlso fetch submodules.
git config --global user.name "Your Name"Set the name that appears on your commits.

Stored in ~/.gitconfig. Leave out --global to set it only for the current repository, for example when you use a different identity for work.

Options

--globalFor your whole user account.
--localOnly this repository (default when --global is missing).
--list --show-originShow all settings and which file they come from.
git config --global user.email "[email protected]"Set the email address on your commits.

Use the same address as your Git hosting account (GitHub, GitLab) so commits are linked to you.

git config --global init.defaultBranch mainUse main as the default name for new repositories.

Applies to new repositories created with git init.

git submodule update --init --recursiveFetch submodules after cloning a repository that uses them.

Submodules are other repositories pinned at a specific commit. A normal clone leaves their folders empty until you run this.

git statusShow which files are changed, staged or untracked.

Run it often. It also tells you which branch you are on and the next useful command.

Options

-sShort output, one line per file.
-bShow branch and tracking info in short output.
git add <file>Stage a file so it is included in the next commit.

Staging lets you build a commit from selected changes instead of everything at once.

Example

git add src/app.js README.md

Options

.Stage everything in the current folder (new, changed and deleted).
-AStage all changes in the whole repository.
-uStage only files Git already tracks.
-pPick changes interactively.
git add -pStage changes interactively, piece by piece.

Git shows each changed hunk and asks what to do. Good for splitting unrelated changes into separate, clean commits.

Options

y / nStage or skip this hunk.
sSplit the hunk into smaller pieces.
eEdit the hunk by hand.
qQuit.
git commit -m "message"Save the staged changes as a commit.

A commit is a snapshot with a message. Write the message in the imperative: โ€œFix login timeoutโ€. Without -m an editor opens.

Example

git commit -m "Add password strength checker"

Options

-aAutomatically stage all modified tracked files first (new files are not included).
-vShow the diff in the message editor.
--no-verifySkip pre-commit hooks.
Careful: --no-verify skips checks such as linting and tests. Only use it when you know why.
git commit --amendChange the most recent commit (message or content).

Stage extra changes first to add them to the last commit, or run it alone to fix the message. It replaces the commit with a new one.

Example

git commit --amend --no-edit

Options

--no-editKeep the existing message.
-m "new"Replace the message.
Careful: This rewrites history. Do not amend a commit you already pushed to a shared branch.
git diffShow unstaged changes in your working files.

Compares your working directory with the staging area. Nothing shown means everything changed is already staged.

Example

git diff main..feature -- src/

Options

--statSummary of changed files and line counts.
--name-onlyOnly list file names.
<a>..<b>Compare two commits or branches.
-- <path>Limit to a file or folder.
git diff --stagedShow what is staged and will go into the next commit.

Review this before committing. Also written as --cached.

git rm <file>Delete a file and stage the deletion.

Options

--cachedStop tracking the file but keep it on disk (useful after adding it to .gitignore).
-rRemove a folder recursively.
git mv <old> <new>Rename or move a file and stage the change.

Equivalent to moving the file yourself and running git add. Git detects renames automatically.

git log --oneline --graph --decorateCompact history with a branch graph.

One line per commit, with branch and tag names. The best quick overview of how branches relate.

Example

git log --oneline --graph --all -n 20

Options

--allInclude all branches, not just the current one.
-n <num>Limit the number of commits.
--author="name"Only commits by this author.
--since="2 weeks ago"Only recent commits.
git log -p <file>History of one file including the changes.

Options

--followKeep following the file across renames.
--statShow changed-line counts instead of full diffs.
git show <commit>Show one commit with its message and changes.

HEAD is the current commit. HEAD~1 is its parent, HEAD~2 the commit before that.

Example

git show HEAD~2
git blame <file>Show who last changed each line and in which commit.

Example

git blame -L 10,25 src/app.js

Options

-L <start>,<end>Only these lines.
-wIgnore whitespace changes.
git reflogHistory of where HEAD has been. Recovers โ€œlostโ€ commits.

After a bad reset or rebase, find the old commit here and return to it with git reset --hard <hash> or create a branch from it. Entries expire after about 90 days.

Example

git branch rescue a1b2c3d
git grep "text"Search tracked files for text.

Faster than grep -r because it only looks at files Git tracks and ignores build output.

Options

-nShow line numbers.
-iIgnore case.
-wMatch whole words.
git shortlog -snCommits per author, most active first.

A quick way to see who contributed how much. -s prints counts instead of commit messages and -n sorts by count.

Options

-sSummary: only counts.
-nSort by number of commits.
-eInclude email addresses.
git bisect startBinary search for the commit that introduced a bug.

Mark a bad commit and a good one. Git checks out the middle; you test and answer good or bad until it finds the culprit.

Example

git bisect start
git bisect bad
git bisect good v1.2.0
# test, then: git bisect good|bad
git bisect reset

Options

bisect resetFinish and return to your original branch.
bisect run <script>Automate with a script that exits 0 for good and non-zero for bad.
git branchList local branches. The current one is marked with *.

Options

-aInclude remote-tracking branches.
-vvShow the last commit and upstream of each branch.
--mergedOnly branches already merged into the current one.
git branch <name>Create a branch at the current commit without switching to it.

The new branch starts where you are now. Move to it with git switch <name>, or create and switch in one step with git switch -c <name>.

git switch <branch>Switch to another branch.

Updates your working files to that branch. Uncommitted changes that conflict will block the switch; commit or stash them first. Older guides use git checkout for this.

git switch -c <branch>Create a new branch and switch to it.

Example

git switch -c feature/login
git branch -d <branch>Delete a branch that is already merged.

Options

-DForce delete even if it is not merged.
Careful: -D throws away unmerged commits. You can still find them through git reflog for a while.
git branch -m <new-name>Rename the current branch.

If the branch was already pushed, push the new name and delete the old one on the remote.

git merge <branch>Merge another branch into the one you are on.

Creates a merge commit when histories have diverged. On conflicts, edit the marked files, git add them and run git commit.

Options

--no-ffAlways create a merge commit, even when a fast-forward is possible.
--squashCombine all changes into one staged change without a merge commit.
--abortCancel a merge that hit conflicts.
git rebase <branch>Replay your commits on top of another branch for a linear history.

Typical use: git rebase main on your feature branch to pick up the latest changes. On conflicts fix the files, git add them and run git rebase --continue.

Options

--continueContinue after resolving conflicts.
--abortCancel and return to how things were.
-iInteractive: reorder, squash or edit commits.
Careful: Rebase rewrites commits. Never rebase branches other people are using.
git rebase -i HEAD~3Clean up the last 3 commits: squash, reword, drop or reorder.

Opens an editor with one line per commit. Change โ€œpickโ€ to squash (s), reword (r), edit (e) or drop (d), save and close.

Careful: Only for commits that are not shared yet.
git cherry-pick <commit>Copy a single commit onto the current branch.

Useful for applying a hotfix to a release branch without merging everything else.

Options

-nApply the changes but do not commit.
-xAdd a line with the original commit hash to the message.
--abortCancel after conflicts.
git worktree add ../hotfix hotfixCheck out another branch in a separate folder at the same time.

Lets you work on a hotfix without stashing or leaving your current branch. Remove it later with git worktree remove ../hotfix.

git remote -vList the remotes and their URLs.

Normally shows origin with a fetch and a push URL. Change a URL later with git remote set-url origin <url>.

git remote add origin <url>Connect your local repository to a remote.

Typical after git init: add origin, then git push -u origin main.

git fetchDownload remote changes without changing your files.

Safe to run any time. Afterwards compare with git log HEAD..origin/main or merge when ready.

Options

--pruneRemove local references to branches deleted on the remote.
--allFetch from all remotes.
git pullFetch remote changes and merge them into your branch.

Options

--rebaseRebase your commits on top instead of creating a merge commit.
--ff-onlyRefuse to pull if it would need a merge.
git pushUpload your commits to the remote branch.

Rejected? Someone pushed first. Pull (or fetch and rebase), then push again.

git push -u origin <branch>Push a new branch and remember it as the upstream.

After this, a plain git push and git pull work for that branch.

git push --force-with-leaseForce push, but only if nobody else pushed in the meantime.

Needed after rebasing or amending a commit that was already pushed to your own branch.

Careful: Plain --force can erase other peopleโ€™s work. Prefer --force-with-lease and never force-push to shared main branches.
git push origin --delete <branch>Delete a branch on the remote.

Only removes the branch on the server. Delete your local copy with git branch -d <branch>. Teammates clean up stale references with git fetch --prune.

git restore <file>Discard unstaged changes in a file and return it to the last commit.
Careful: The changes are lost for good. Git cannot bring back uncommitted edits.
git restore --staged <file>Unstage a file but keep your edits.

Moves the file from the staging area back to your working files. Nothing is lost. It replaces the older git reset HEAD <file>.

git restore --source=<commit> <file>Bring back a file as it was in an older commit.

Example

git restore --source=HEAD~3 src/app.js
git reset --soft HEAD~1Undo the last commit but keep the changes staged.

Handy when you committed too early or want to redo the message or split the commit.

git reset HEAD~1Undo the last commit and unstage the changes, keeping the files.

This is the default โ€œmixedโ€ reset. Your edits stay in the working directory.

git reset --hard HEAD~1Undo the last commit and delete its changes.
Careful: Destroys uncommitted work. If you reset too far, look in git reflog to get the commit back.
git revert <commit>Create a new commit that undoes an earlier commit.

Safe for shared branches because history is not rewritten. For a merge commit add -m 1 to pick the mainline parent.

Options

-nApply the undo without committing.
-m 1Required when reverting a merge commit.
git clean -fdDelete untracked files and folders.

Always preview first with git clean -nd.

Options

-nDry run: only show what would be removed.
-dInclude folders.
-xAlso remove ignored files.
-fRequired to actually delete.
Careful: Deleted files do not go to the recycle bin and cannot be recovered through Git.
git stashShelve your uncommitted changes so you can switch tasks.

Example

git stash push -u -m "wip login form"

Options

-uInclude untracked files.
-m "msg"Label the stash so you recognise it later.
git stash popRe-apply the latest stash and remove it from the list.

Use git stash apply to keep the stash after applying it.

git stash listList your stashes, newest first.

Each entry has an index such as stash@{0}, the branch and your label. Inspect one with git stash show -p stash@{0}, apply a specific one with git stash apply stash@{1}.

git tag -a v1.0.0 -m "Release 1.0.0"Mark a commit as a release with an annotated tag.

Annotated tags store author, date and message. A lightweight tag (git tag v1.0.0) is only a pointer.

git push --tagsUpload all local tags to the remote.

Tags are not pushed by a normal git push.

The descriptions in this reference are currently in English.

About this tool

Search or filter by group and copy a command with one click. Open a command to see what it does, an example, its main options and warnings where it can do damage.

๐Ÿ”’ Your input stays in your browser and is never sent anywhere.

Frequently asked questions

What is the difference between reset and revert?

Reset moves the branch pointer and rewrites history. Revert adds a new commit that undoes a change and is safe on shared branches.

What to do next

More in this section

Also useful

Found a problem or have a suggestion? Tell us