Git commands cheat sheet: the 40 commands you actually use
By Pull This RepoPosted 5 min read
Short answer
This Git commands cheat sheet covers the 40 commands most people use: git config and git clone to set up, git add and git commit to save, git switch and git merge for branches, git pull and git push to sync, git restore, git reset and git stash to undo, and git log to look back. Each line was checked against the Git docs.
This Git commands cheat sheet lists the 40 commands that cover nearly all day-to-day Git work, grouped by the job you are trying to do. Each one gets a single line saying what it does. Below the tables is a plain block you can print or paste into a notes file, plus a short section on git ls-remote. Every command and flag was checked against the official Git reference on git-scm.com on 22 November 2026. Replace anything in angle brackets, like <file>, with your own value.
How do you set up Git and get a repo?
Five commands get you from a fresh install to a working copy:
| # | Command | What it does |
|---|---|---|
| 1 | git config --global user.name "Your Name" | sets the name recorded in your commits |
| 2 | git config --global user.email you@example.com | sets the email recorded in your commits |
| 3 | git config --list --show-origin | shows every setting and which file it came from |
| 4 | git init | turns the current folder into a new, empty repo |
| 5 | git clone <url> | copies a remote repo, with its full history, into a new folder |
git init -b main names the first branch main instead of the default. For the full install and first-time setup, see how to install Git. For every way to copy a project down, see how to clone a GitHub repo.
Which basic Git commands save your work?
Saving in Git is two steps: stage the changes you want, then commit them.
| # | Command | What it does |
|---|---|---|
| 6 | git status | shows changed, staged and untracked files |
| 7 | git status -s | the same, in a short one-line-per-file format |
| 8 | git add <file> | stages a file for the next commit (git add . stages the whole folder) |
| 9 | git add -p | lets you pick which parts of a file to stage, hunk by hunk |
| 10 | git diff | shows changes you have not staged yet |
| 11 | git diff --staged | shows changes that are staged and about to be committed |
| 12 | git commit -m "message" | saves the staged changes as a new commit |
| 13 | git commit -am "message" | stages modified and deleted tracked files, then commits; new files are skipped |
| 14 | git commit --amend | replaces the last commit with a new one, for a fixed message or a forgotten file |
| 15 | git rm <file> | deletes a file and stages the deletion |
git diff --staged and git diff --cached are the same command; the docs call --staged a synonym. To take a file back out of the staging area, see how to unstage a file in Git.
How do you work with branches?
A branch is a separate line of commits. These seven handle almost everything:
| # | Command | What it does |
|---|---|---|
| 16 | git branch | lists local branches and marks the current one |
| 17 | git switch <branch> | moves to an existing branch |
| 18 | git switch -c <branch> | creates a new branch and moves to it |
| 19 | git branch -d <branch> | deletes a branch, but only if it is fully merged |
| 20 | git branch -D <branch> | force deletes a branch, merged or not |
| 21 | git merge <branch> | joins another branch's commits into the current one |
| 22 | git rebase <branch> | replays your commits on top of another branch |
The git branch docs describe -D as a shortcut for --delete --force, so check git log before you use it. To copy one file across from a different branch without merging, see how to get a file from another branch in Git.
How do you sync with GitHub?
A remote is a saved address for another copy of the repo, usually origin on GitHub.
| # | Command | What it does |
|---|---|---|
| 23 | git remote -v | lists remotes and their URLs |
| 24 | git remote add origin <url> | links your local repo to a remote |
| 25 | git fetch | downloads new commits and branches without changing your files |
| 26 | git pull | fetches, then integrates the remote branch into your current branch |
| 27 | git push | uploads your commits to the remote branch |
| 28 | git push -u origin <branch> | pushes a new branch and sets it as the upstream, so plain git pull and git push work next time |
git fetch --prune also removes remote-tracking branches that were deleted on GitHub, which keeps git branch -a tidy.
How do you undo things in Git?
Pick the command by how far the change has travelled:
| # | Command | What it does |
|---|---|---|
| 29 | git restore <file> | throws away unstaged edits to a file |
| 30 | git restore --staged <file> | unstages a file but keeps your edits |
| 31 | git reset --soft HEAD~1 | removes the last commit and keeps its changes staged |
| 32 | git reset --hard <commit> | moves the branch back and overwrites your files to match; uncommitted work is lost |
| 33 | git revert <commit> | records a new commit that reverses an old one, safe for shared branches |
| 34 | git stash | shelves uncommitted changes and gives you a clean working folder |
| 35 | git stash pop | brings the latest stashed changes back and removes them from the stash |
The git reset docs warn that --hard overwrites files and directories and may overwrite untracked files, so commit or stash first if you are unsure. Stashing has more options than these two lines; our git stash guide covers untracked files, single files and getting a dropped stash back.
How do you see what happened?
These five answer "who changed what, and when":
| # | Command | What it does |
|---|---|---|
| 36 | git log --oneline | shows history, one short line per commit |
| 37 | git log --oneline --graph --all | draws every branch as a text graph |
| 38 | git show <commit> | shows one commit's message and changes |
| 39 | git blame <file> | shows which commit last changed each line of a file |
| 40 | git reflog | lists where HEAD and your branches have pointed, useful for finding lost commits |
git show also prints a file as it was in any branch or commit, for example git show main:README.md.
What does git ls-remote do?
git ls-remote lists the branches and tags in a remote repo, with the commit ID each one points to, without cloning or fetching. It is handy for checking whether a branch exists on GitHub or which commit a tag points to.
# everything on your origin remote
git ls-remote origin
# only branches, or only tags, on any repo URL
git ls-remote --branches https://github.com/OWNER/REPO
git ls-remote --tags https://github.com/OWNER/REPO
# one branch by name
git ls-remote origin mainEach output line is a commit ID, a tab, then the ref name, such as refs/heads/main. The docs note that --heads still works as a deprecated synonym for --branches. Add --exit-code in scripts: it exits with status 2 when no matching refs are found, so you can test whether a branch exists.
What is the printable version?
Copy this block into a notes file or print it:
SET UP
git config --global user.name "Your Name"
git config --global user.email you@example.com
git config --list --show-origin
git init
git clone <url>
SAVE
git status
git status -s
git add <file>
git add -p
git diff
git diff --staged
git commit -m "message"
git commit -am "message"
git commit --amend
git rm <file>
BRANCH
git branch
git switch <branch>
git switch -c <branch>
git branch -d <branch>
git branch -D <branch>
git merge <branch>
git rebase <branch>
SYNC
git remote -v
git remote add origin <url>
git fetch
git pull
git push
git push -u origin <branch>
UNDO
git restore <file>
git restore --staged <file>
git reset --soft HEAD~1
git reset --hard <commit>
git revert <commit>
git stash
git stash pop
INSPECT
git log --oneline
git log --oneline --graph --all
git show <commit>
git blame <file>
git reflog
REMOTE CHECK
git ls-remote originWhy use git switch and git restore instead of git checkout?
Because each does one job. The Git 2.23 release notes say the two commands were added to split "checking out a branch" and "checking out paths" out of the single git checkout command. git checkout <branch> and its file-restoring form still work, which is why you see them in older guides, but git switch and git restore make it harder to throw away a file when you meant to change branches.
What is the catch?
A cheat sheet tells you the command, not the situation. Three of these, git reset --hard, git branch -D and git commit --amend on a commit you already pushed, rewrite or delete work, so run git status and git log --oneline before them. When something goes wrong, git reflog is usually the way back. For the full options of any command, run git help <command> or read its page on git-scm.com.
Pull it. Stash it.
Questions people ask
What are the basic Git commands?
The everyday loop is git status to see what changed, git add to stage it, git commit -m to save it, and git push to send it to GitHub. git pull brings other people's work in, git switch moves between branches, and git log shows the history. Most other commands handle less common jobs.
What is the difference between git fetch and git pull?
git fetch downloads new commits and branches from the remote but leaves your own branch alone. git pull runs git fetch first and then integrates the remote branch into your current branch, by merging or rebasing depending on your settings. Use fetch when you want to look before you combine.
What does git ls-remote do?
It lists the references in a remote repository, such as branches and tags, with the commit ID each one points to. It talks to the remote directly, so you can see what is on GitHub without cloning or fetching. Add --branches or --tags to limit the list.
How do I undo the last commit in Git?
Run git reset --soft HEAD~1 to remove the last commit but keep its changes staged, ready to commit again. If the commit is already pushed and shared, use git revert with the commit ID instead, which records a new commit that reverses it without rewriting history.
What is the difference between git switch and git checkout?
Git 2.23 added git switch and git restore to split the two jobs git checkout used to do. git switch only changes branches, and git restore only restores files. git checkout still does both, so older guides and scripts that use it keep working.
Sources
- 1git-scm.com: Git reference documentation (read 2026-11-22)
- 2git-scm.com: git config (read 2026-11-22)
- 3git-scm.com: git init (read 2026-11-22)
- 4git-scm.com: git clone (read 2026-11-22)
- 5git-scm.com: git status (read 2026-11-22)
- 6git-scm.com: git add (read 2026-11-22)
- 7git-scm.com: git diff (read 2026-11-22)
- 8git-scm.com: git commit (read 2026-11-22)
- 9git-scm.com: git rm (read 2026-11-22)
- 10git-scm.com: git branch (read 2026-11-22)
- 11git-scm.com: git switch (read 2026-11-22)
- 12git-scm.com: git merge (read 2026-11-22)
- 13git-scm.com: git rebase (read 2026-11-22)
- 14git-scm.com: git remote (read 2026-11-22)
- 15git-scm.com: git fetch (read 2026-11-22)
- 16git-scm.com: git pull (read 2026-11-22)
- 17git-scm.com: git push (read 2026-11-22)
- 18git-scm.com: git ls-remote (read 2026-11-22)
- 19git-scm.com: git restore (read 2026-11-22)
- 20git-scm.com: git reset (read 2026-11-22)
- 21git-scm.com: git revert (read 2026-11-22)
- 22git-scm.com: git stash (read 2026-11-22)
- 23git-scm.com: git log (read 2026-11-22)
- 24git-scm.com: git show (read 2026-11-22)
- 25git-scm.com: git blame (read 2026-11-22)
- 26git-scm.com: git reflog (read 2026-11-22)
- 27Git 2.23.0 release notes (read 2026-11-22)
More repos like this
Get tomorrow's repo in your feed
One free, open source repo a day as a short video, shown working, with the catch named. Pull it. Stash it.
