Home › Guides › Reference › Git Cheat Sheet

Git Cheat Sheet

The Git commands you use every day, grouped by task: set up, commit, branch, merge, undo, stash, tag and share, with notes on safe and risky commands.

By the tools4free.dev team · Last reviewed 4 December 2026

How to read this cheat sheet

Git records snapshots of your project. Three areas matter for almost every command. The working tree holds the files you edit. The index, also called the staging area, holds the changes you have chosen for the next commit. The repository holds the committed history. Commands move changes between these areas: git add moves from working tree to index, git commit moves from index to repository, and git restore or git checkout move the other way.

A branch is a movable name that points to a commit, and HEAD is the name for the commit or branch you currently have checked out. A remote, usually named origin, is another copy of the repository, for example on a hosting service. Most of the commands here are safe to run and easy to undo. Commands that discard work or rewrite history are marked careful in their notes, and a short section near the end explains why.

The tables use short explanations, and the code blocks show typical sequences. Replace names in angle brackets, such as <branch>, with your own. Git's own help is always at hand: git help <command> or git <command> --help opens the manual page, and the full reference is at git-scm.com/docs. To compare two versions of a text outside Git, use the diff checker. For shell commands around Git, see the Linux command cheat sheet.

The commands you will use most

git status, git add, git commit, git diff, git log, git switch, git pull, git push, git stash. Run git status often: it tells you where you are and often suggests the next command.

Set up and configure

CommandWhat it does
git config --global user.name "Your Name"Sets the name recorded on your commits.
git config --global user.email "you@example.com"Sets the email recorded on your commits.
git config --global init.defaultBranch mainNames the first branch of new repositories main.
git config --listShows all settings in effect.
git config --global alias.st statusCreates a short alias, so git st runs git status.

Create or copy a repository

CommandWhat it does
git initTurns the current folder into a new repository.
git clone <url>Copies a remote repository, with its full history, into a new folder.
git clone --depth 1 <url>Copies only the latest commit. Faster for large repositories when you do not need history.

Record changes

CommandWhat it does
git statusShows changed, staged and untracked files and the current branch.
git add <file>Stages a file for the next commit.
git add -pStages changes piece by piece, so you choose which parts to include.
git commit -m "message"Commits the staged changes with a message.
git commit --amendReplaces the last commit with a new one that includes the staged changes or a new message. Careful if it was already shared.
git rm <file>Deletes a file and stages the deletion.
git mv <old> <new>Renames or moves a file and stages the change.
git status
git add src/app.js
git commit -m "Fix login redirect"

Look at what changed

CommandWhat it does
git diffShows unstaged changes compared with the index.
git diff --stagedShows staged changes compared with the last commit.
git logLists commits, newest first.
git log --oneline --graph --decorateCompact history with a branch graph.
git log -p <file>Shows the history of one file with the changes in each commit.
git show <commit>Shows one commit and its changes.
git blame <file>Shows who last changed each line.
git shortlog -snCounts commits by author.

Branches

CommandWhat it does
git branchLists local branches. The current one is marked with an asterisk.
git branch <name>Creates a branch at the current commit.
git switch <name>Moves to an existing branch.
git switch -c <name>Creates a branch and moves to it in one step.
git branch -d <name>Deletes a branch that is fully merged. Use -D to delete it anyway. Careful.
git branch -m <new-name>Renames the current branch.
git checkout <name>The older command to switch branches. It still works and also restores files.
git switch -c feature/search
# edit, add and commit
git switch main
git merge feature/search

Merge and rebase

CommandWhat it does
git merge <branch>Combines the named branch into the current one. Creates a merge commit unless the history allows a fast-forward.
git merge --abortStops a merge that has conflicts and returns to the state before it.
git rebase <branch>Replays your commits on top of another branch for a straight history. It rewrites commits; careful on shared branches.
git rebase --continueCarries on after you have fixed a conflict during a rebase.
git rebase --abortCancels a rebase and returns to the state before it.
git cherry-pick <commit>Applies the changes from one commit onto the current branch.

When Git cannot merge automatically, it marks the conflict in the file with <<<<<<<, ======= and >>>>>>>. Edit the file to the result you want, remove the markers, run git add on the file, then finish with git commit (or git rebase --continue).

Undo and recover

CommandWhat it does
git restore <file>Discards unstaged changes in a file. Careful: the changes are lost.
git restore --staged <file>Unstages a file but keeps your edits.
git revert <commit>Makes a new commit that reverses an earlier one. Safe for shared history.
git reset --soft HEAD~1Undoes the last commit but keeps its changes staged.
git reset HEAD~1Undoes the last commit and keeps its changes in the working tree, unstaged.
git reset --hard <commit>Moves the branch to a commit and discards all uncommitted changes. Careful.
git clean -nPreviews which untracked files would be deleted. Run before git clean -f.
git reflogLists where HEAD has been. Often lets you recover commits that seem lost.

Stash: park work for later

CommandWhat it does
git stashSaves uncommitted changes and cleans the working tree.
git stash -uAlso stashes untracked files.
git stash listLists saved stashes.
git stash popReapplies the latest stash and removes it from the list.
git stash applyReapplies the latest stash and keeps it in the list.
git stash dropDeletes the latest stash.

Work with remotes

CommandWhat it does
git remote -vLists remotes and their URLs.
git remote add origin <url>Adds a remote named origin.
git fetchDownloads new commits and branches without changing your files.
git pullFetches and then integrates the changes into your current branch.
git pull --rebaseFetches and replays your local commits on top, giving a linear history.
git pushUploads your commits on the current branch.
git push -u origin <branch>Pushes a new branch and sets it to track the remote one.
git push --force-with-leaseOverwrites the remote branch only if nobody else has pushed to it. Safer than --force. Careful.

Tags

CommandWhat it does
git tagLists tags.
git tag v1.0.0Creates a lightweight tag on the current commit.
git tag -a v1.0.0 -m "Release 1.0.0"Creates an annotated tag with a message.
git push origin v1.0.0Uploads one tag. Tags are not pushed by default.

Ignoring files

List files that should never be committed, such as build output, local settings and secrets, in a file called .gitignore. Each line is a pattern.

node_modules/
*.log
.env
dist/

A pattern only affects files that are not already tracked. To stop tracking a file that was committed, use git rm --cached <file> and commit. A secret that has been pushed must be treated as exposed: change it, even if you delete the file later.

Safe and risky commands

RiskExamplesWhy
Safestatus, diff, log, show, branch, fetch, stashRead or add information without discarding work.
Mostly safeadd, commit, switch, merge, pull, revert, tagChange state, but history stays recoverable.
Carefulreset --hard, restore, clean -f, branch -DCan permanently discard uncommitted work.
Careful, shared historyrebase, commit --amend, push --force-with-leaseRewrite commits other people may already have.

Common mistakes

Sources