Command reference
Read the current state with git status and a diff before you change it. COMMIT, MERGE, BAD, GOOD and URL are placeholders; branch and file names are examples. Every command here works the same in Git 2.40 to 2.52.
| You want to |
Use |
Not |
| Record a change |
git add → git diff --staged → git commit |
git commit -a without looking |
| See what others have published |
git fetch → git log --oneline --graph --all |
git pull blindly |
| Bring in others' work |
git pull --ff-only, or choose --rebase / --no-rebase |
git pull without choosing a strategy |
| Publish after a rejected push |
fetch → integrate → test → git push |
git push --force |
| Throw away unstaged edits in one file |
git restore README.md |
git reset --hard |
| Unstage a file but keep the edit |
git restore --staged README.md |
git rm |
| Undo a commit others already have |
git revert COMMIT |
git reset + force push |
| Tidy commits only you have |
git commit --amend, git rebase -i, git reset --soft |
rewriting a shared branch |
| Put work aside for a moment |
git stash push -u, or a commit on a branch |
deleting files |
| Rescue a commit that looks lost |
git reflog → git branch rescue COMMIT → check it |
cleaning up first |
| Find the commit that broke something |
git bisect |
reading every commit |
Create and inspect
| Command |
What it does |
git --version |
Show the installed Git version. |
git init -b main |
Create a repository whose first branch is called main. |
git clone URL |
Copy a repository with its whole history, name the remote origin and check out its default branch. |
git config --global user.name "Name" |
Set the author name on your commits. It does not log you in anywhere. |
git config --global user.email "you@example.invalid" |
Set the author email on your commits. |
git config --show-origin --list |
Show every setting and the file it comes from. |
git status |
Show branch, upstream status, staged, unstaged and untracked files. |
git status --short |
File status in two columns: staging area on the left, working tree on the right. |
git diff |
Compare tracked working files with the staging area. Untracked files are not shown. |
git diff --staged |
Compare the staging area with HEAD: what the next commit will record. |
git show HEAD |
Show a commit's metadata and its change. |
git log --oneline --graph --all |
Draw the history reachable from every ref: branches, remote-tracking branches and tags. |
git log main..feature |
Commits on feature that main does not have. |
git diff main...feature |
The change on feature since it parted from main. |
git merge-base main feature |
Show the merge base: the best common ancestor of two commits. |
git blame README.md |
Show which commit last changed each line. |
git log -S "text" |
Find commits that changed how many times the text occurs, that is, added or removed it. |
git cat-file -p HEAD |
Print the raw commit object: tree, parents, author, message. |
Record small changes
| Command |
What it does |
git add README.md |
Copy this version of the file into the staging area. Later edits are not staged automatically. |
git add -p |
Choose hunks one at a time for a focused commit. |
git add -A |
Stage new, changed and deleted files everywhere. Check the diff first. |
git commit -m "Explain why" |
Record the staging area as a new commit and move the current branch. Nothing is sent anywhere. |
git commit --amend |
Replace the last commit with a new one. Only for commits nobody else has. |
git rm old.txt |
Delete a tracked file and stage the deletion. |
git mv old.txt new.txt |
Rename a file and stage it. Git detects renames by comparing snapshots. |
git check-ignore -v build/output.log |
Show which ignore rule matches. Ignoring does not affect already tracked files. |
Branches and integration
| Command |
What it does |
git branch |
List local branches. git branch -vv adds upstream and ahead/behind. |
git branch feature |
Create a branch at HEAD without switching to it. |
git switch -c feature |
Create a branch at HEAD and switch to it. |
git switch main |
Switch branch; refuses if uncommitted edits would be overwritten. |
git switch --detach HEAD~1 |
Look at an old commit. Create a branch if you want to keep work made there. |
git branch -d feature |
Delete a merged branch. Refuses if its commits are not merged. |
git merge feature |
Integrate feature: fast-forward if possible, otherwise a merge commit. |
git merge --ff-only feature |
Integrate only by fast-forward; refuse if the lines have split. |
git merge --no-ff feature |
Make a merge commit even when a fast-forward is possible. |
git merge --squash feature |
Stage feature's combined change without committing; the next commit has one parent. |
git merge --abort |
Abandon a merge in progress and return to where you were. |
git merge --continue |
Finish a merge after resolving and staging the conflicts. |
git rebase main |
Replay your branch's commits on top of main. They get new IDs. |
git rebase -i main |
Reorder, reword, squash, fixup or drop your own unpublished commits. |
git rebase --continue |
Continue after resolving and staging a conflict. |
git rebase --abort |
Return to the state before the rebase started. |
git rebase --skip |
Drop the commit that is being replayed and continue. Know which work you leave out. |
git range-diff main feature@{1} feature |
Compare a series of commits before and after a rebase. |
git cherry-pick COMMIT |
Copy one commit's change onto the current branch as a new commit. |
git tag try-1 |
Create a lightweight tag: just a name for the HEAD commit. |
git tag -a v1.0 -m "Release 1.0" |
Create an annotated tag with a message, for releases. |
Collaboration and remotes
| Command |
What it does |
git remote -v |
List remote names and their addresses. |
git remote add origin URL |
Give an address a name. Nothing is downloaded. |
git remote rename origin shared |
Rename a remote, its remote-tracking branches and upstream settings. |
git remote set-url origin URL |
Change a remote's address. |
git ls-remote origin |
Ask the remote which refs it has right now. |
git fetch |
Download new commits and move origin/… refs. Your branches and files stay as they are. |
git fetch --prune |
Also remove origin/… refs whose branches were deleted in the remote. |
git pull --ff-only |
Fetch, then fast-forward only. The fetch half happens even if the integration is refused. |
git pull --no-rebase |
Fetch, then merge. |
git pull --rebase |
Fetch, then replay your local commits that the upstream lacks on top. Only for commits nobody else has. |
git push -u origin feature |
Publish a branch and make origin/feature its upstream. |
git push |
Send the current branch to its upstream branch of the same name. Accepted only as a fast-forward. |
git branch -u origin/main |
Set the upstream of the current branch. |
git push origin --delete feature |
Delete a branch in the remote, by agreement. Other clones keep theirs. |
git push origin v1.0 |
Send one tag. An ordinary push does not send tags. |
git push --force-with-lease |
Overwrite the remote branch only if it is still where your origin/… says. For your own rewritten branch, never for a shared one. |
Undo and rescue
| Command |
What it does |
git restore README.md |
Replace the working file with the staged version. Unstaged edits are lost. |
git restore --staged README.md |
Unstage: copy the HEAD version into the staging area, keep the working file. |
git restore --source HEAD~1 -- README.md |
Bring an older version of a file into the working tree without moving HEAD. |
git reset --soft HEAD~1 |
Move the branch back; keep the staging area and working tree. |
git reset HEAD~1 |
Move the branch back and reset the staging area; keep the working tree (--mixed is the default). |
git reset --keep HEAD~1 |
Like --hard for the commits you move past, but keep your uncommitted edits; refuses if they would be overwritten. |
git revert COMMIT |
Add a new commit that undoes an old one. Safe on shared history. |
git revert -m 1 MERGE |
Undo a merge commit, keeping the side of its first parent. |
git reflog |
List where HEAD has been. Local only, and entries expire. |
git branch rescue HEAD@{1} |
Pin a commit from the reflog to a branch so it cannot get lost. |
git stash push -u |
Put tracked edits and untracked files aside. Ignored files need -a. |
git stash list |
List stash entries. They stay local; an ordinary push does not send them. |
git stash apply |
Reapply the newest stash and keep the entry. |
git stash pop |
Reapply and drop the entry; it is kept if there is a conflict. |
git clean -nd |
Preview which untracked files and folders would be deleted. |
Investigate and scale
| Command |
What it does |
git bisect start BAD GOOD |
Start a binary search between a bad and a good commit. |
git bisect good / git bisect bad |
Mark the checked-out commit; Git checks out the next candidate halfway through what remains. |
git bisect run sh check.sh |
Let a script decide: exit 0 is good, 1–127 except 125 is bad, 125 skips. |
git bisect reset |
End the search and return to where you started. |
git worktree add -b fix ../fix |
Check out a new branch in a second folder that shares the same repository. |
git worktree remove ../fix |
Remove a clean extra worktree. |
git check-attr -a -- README.md |
Show the attributes, such as line-ending and diff handling, that apply to a file. |
git sparse-checkout set src docs |
Check out only these folders plus the top-level files. History is unchanged. |
git submodule update --init --recursive |
Fetch and check out the pinned versions of submodules. |
git lfs track "*.psd" |
Add a Git LFS tracking rule to .gitattributes, and commit it with the files. Git LFS must be installed separately. |
git bundle create backup.bundle --all |
Write every ref and its history into one file. Uncommitted work is not included. |
git bundle verify backup.bundle |
Check that a bundle is valid and that this repository has any commits it builds on. Practise restoring from it with git clone backup.bundle. |
git fsck |
Check the object database. Dangling or unreachable objects are normal, not damage. |
Danger zone
These commands throw work away or overwrite history. Before you run one, check whether there is a safer tool for the job.
| Command |
What can be lost |
Safer alternative |
git reset --hard |
Every uncommitted change in tracked files, and untracked files in the way of tracked ones, with no undo. Committed work can still be found in git reflog. |
git stash push -u first, or git reset --keep, or git restore for a single file. On shared history: git revert. |
git clean -fd |
Untracked files and folders. Git keeps no copy of their current content, so nothing can bring it back. |
git clean -nd to preview, git clean -id to choose, or git stash push -u. |
git push --force |
Commits on the remote branch that you do not have, usually a colleague's. |
After a rejection: fetch, integrate and push normally. To update your own rewritten branch: git push --force-with-lease, optionally with --force-if-includes (Git 2.30+). |
The same caution applies to git branch -D, git checkout -- . and git stash drop. When something has gone wrong, stop, run git status and git reflog, and create a branch before you clean up.
Official sources
Git reference manual · Pro Git · git push · git reset · git rebase