Git cheat sheet

The Git commands you need most, grouped by task and explained in plain words. Search for what you want to do, such as undo or rename, and click a command to copy it.

Click any entry to copy it.

Set up

CommandWhat it does
git config --global user.name "Your Name"Set the name shown on your commits
git config --global user.email "you@example.com"Set the email shown on your commits
git config --global init.defaultBranch mainName the first branch main in new repositories
git config --listShow your current settings

Start a repository

CommandWhat it does
git initTurn the current folder into a repository
git clone <url>Download a repository and its history
git clone --depth 1 <url>Download only the latest version, which is faster

Everyday work

CommandWhat it does
git statusShow changed, staged and untracked files
git add <file>Stage a file for the next commit
git add .Stage every change in the current folder
git add -pChoose which parts of each file to stage
git commit -m "Message"Save the staged changes as a commit
git commit -am "Message"Stage all tracked files and commit in one step
git diffShow changes that are not staged yet
git diff --stagedShow changes that are staged
git rm <file>Delete a file and stage the deletion
git mv <old> <new>Rename or move a file

Branches

CommandWhat it does
git branchList local branches
git branch -aList local and remote branches
git switch <branch>Switch to a branch
git switch -c <branch>Create a branch and switch to it
git branch -m <new-name>Rename the current branch
git branch -d <branch>Delete a branch that has been merged
git branch -D <branch>Delete a branch even if it has not been merged
git merge <branch>Merge a branch into the current one
git merge --abortStop a merge that has conflicts and go back
git rebase <branch>Replay your commits on top of another branch
git rebase --continueCarry on after fixing a conflict during a rebase
git cherry-pick <commit>Copy one commit onto the current branch

Remotes

CommandWhat it does
git remote -vList remotes and their addresses
git remote add origin <url>Connect the repository to a remote
git fetchDownload new commits without changing your files
git pullFetch and merge the remote branch
git pull --rebaseFetch and replay your commits on top
git pushUpload your commits
git push -u origin <branch>Upload a new branch and remember where it goes
git push --force-with-leaseOverwrite the remote branch, but only if nobody else has pushed
git push origin --delete <branch>Delete a branch on the remote

Undo

CommandWhat it does
git restore <file>Throw away unstaged changes to a file
git restore --staged <file>Unstage a file and keep the changes
git commit --amendChange the last commit or its message
git reset --soft HEAD~1Undo the last commit and keep its changes staged
git reset --hard HEAD~1Undo the last commit and delete its changes
git reset --hard origin/mainMake the branch match the remote exactly
git revert <commit>Add a new commit that reverses an old one. Safe on shared branches
git clean -fdDelete untracked files and folders
git reflogList where HEAD has been, to recover lost commits

Stash

CommandWhat it does
git stashPut uncommitted changes aside
git stash -uStash untracked files too
git stash listList what you have stashed
git stash popBring back the latest stash and remove it from the list
git stash applyBring back the latest stash and keep it in the list
git stash dropDelete the latest stash

History

CommandWhat it does
git log --oneline --graph --allCompact history of every branch as a graph
git log -p <file>History of one file with the changes
git log --author="Name"Commits by one person
git log -S "text"Commits that added or removed some text
git show <commit>Show one commit and its changes
git blame <file>Show who last changed each line
git diff <branch-a>..<branch-b>Compare two branches
git bisect startBegin a search for the commit that introduced a bug

Tags

CommandWhat it does
git tagList tags
git tag -a v1.0.0 -m "Version 1.0.0"Create a tag with a message
git push --tagsUpload your tags
git tag -d v1.0.0Delete a local tag

Ignore files

CommandWhat it does
git rm --cached <file>Stop tracking a file but keep it on disk
git check-ignore -v <file>Show which rule ignores a file
git status --ignoredList ignored files

How to use the cheat sheet

  1. Type what you want to do, such as undo, rename or stash.
  2. Click a command to copy it.
  3. Replace the parts in angle brackets, such as <branch>, with your own names.

The three places your changes live

Git keeps your work in three places. The working folder holds the files you edit. The staging area holds the changes you have picked for the next commit with git add. The repository holds the commits themselves. Most confusion goes away once you know which of the three a command touches: git restore changes the working folder, git restore --staged changes the staging area, and git reset moves commits.

Undoing things safely

You want toUse
Fix the message of the last commitgit commit --amend
Undo the last commit but keep the workgit reset --soft HEAD~1
Undo a commit that is already pushedgit revert <commit>
Throw away changes to one filegit restore <file>
Get back a commit you lostgit reflog, then git switch -c rescue <commit>

Commands with --hard or clean delete work that was never committed, and Git cannot bring that back. Run git status first.

Merge or rebase?

git merge joins two branches with a merge commit and leaves history as it happened. git rebase rewrites your commits so they sit on top of the other branch, which gives a straight line of history. A simple rule: rebase your own branch before sharing it, and never rebase commits other people already have.

Need to keep files out of a repository? Use the .gitignore generator. To compare two versions of a file outside Git, try the diff and merge tool. There is also a regex cheat sheet.

Frequently asked questions

How do I undo the last commit in Git?

Run git reset --soft HEAD~1 to undo it and keep your changes. If the commit is already pushed, use git revert instead so you do not rewrite shared history.

What is the difference between git fetch and git pull?

Fetch downloads new commits and leaves your files alone. Pull does a fetch and then merges the new commits into your branch.

What is the difference between git switch and git checkout?

They do the same job for branches. Switch is the newer command and only changes branches, which makes it harder to lose work by accident.

How do I delete a branch?

Use git branch -d name for a local branch and git push origin --delete name for the copy on the remote.

Is git push --force safe?

Plain --force can erase commits someone else pushed. Use --force-with-lease, which refuses to push if the remote has changed.