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 often: it tells you where you are and often suggests the next command.Set up and configure
| Command | What 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 main | Names the first branch of new repositories main. |
git config --list | Shows all settings in effect. |
git config --global alias.st status | Creates a short alias, so git st runs git status. |
Create or copy a repository
| Command | What it does |
|---|---|
git init | Turns 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
| Command | What it does |
|---|---|
git status | Shows changed, staged and untracked files and the current branch. |
git add <file> | Stages a file for the next commit. |
git add -p | Stages changes piece by piece, so you choose which parts to include. |
git commit -m "message" | Commits the staged changes with a message. |
git commit --amend | Replaces 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
| Command | What it does |
|---|---|
git diff | Shows unstaged changes compared with the index. |
git diff --staged | Shows staged changes compared with the last commit. |
git log | Lists commits, newest first. |
git log --oneline --graph --decorate | Compact 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 -sn | Counts commits by author. |
Branches
| Command | What it does |
|---|---|
git branch | Lists 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
| Command | What 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 --abort | Stops 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 --continue | Carries on after you have fixed a conflict during a rebase. |
git rebase --abort | Cancels 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
| Command | What 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~1 | Undoes the last commit but keeps its changes staged. |
git reset HEAD~1 | Undoes 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 -n | Previews which untracked files would be deleted. Run before git clean -f. |
git reflog | Lists where HEAD has been. Often lets you recover commits that seem lost. |
Stash: park work for later
| Command | What it does |
|---|---|
git stash | Saves uncommitted changes and cleans the working tree. |
git stash -u | Also stashes untracked files. |
git stash list | Lists saved stashes. |
git stash pop | Reapplies the latest stash and removes it from the list. |
git stash apply | Reapplies the latest stash and keeps it in the list. |
git stash drop | Deletes the latest stash. |
Work with remotes
| Command | What it does |
|---|---|
git remote -v | Lists remotes and their URLs. |
git remote add origin <url> | Adds a remote named origin. |
git fetch | Downloads new commits and branches without changing your files. |
git pull | Fetches and then integrates the changes into your current branch. |
git pull --rebase | Fetches and replays your local commits on top, giving a linear history. |
git push | Uploads 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-lease | Overwrites the remote branch only if nobody else has pushed to it. Safer than --force. Careful. |
Tags
| Command | What it does |
|---|---|
git tag | Lists tags. |
git tag v1.0.0 | Creates 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.0 | Uploads 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
| Risk | Examples | Why |
|---|---|---|
| Safe | status, diff, log, show, branch, fetch, stash | Read or add information without discarding work. |
| Mostly safe | add, commit, switch, merge, pull, revert, tag | Change state, but history stays recoverable. |
| Careful | reset --hard, restore, clean -f, branch -D | Can permanently discard uncommitted work. |
| Careful, shared history | rebase, commit --amend, push --force-with-lease | Rewrite commits other people may already have. |
Common mistakes
- Committing to the wrong branch. Run
git statusbefore you commit. If you did, create a branch at that commit withgit branch <name>, then move the original branch back withgit reset. - Using git add . without looking. It stages everything, including files you meant to leave out. Check with
git status. - Rewriting shared history. Do not rebase or amend commits that others have pulled, or force-push to shared branches.
- Vague commit messages. "Fix" tells a reader nothing. State what changed and why, in the imperative: "Fix login redirect".
- Committing secrets. Passwords and keys stay in history after deletion. Keep them out of the repository.
- Panicking after a bad reset. Check
git reflogfirst. Committed work is usually still there.