Git Commands Cheat Sheet: Commit, Branch, Merge, Undo and More
The Git commands you use most, grouped by job, with an example for each. Names such as notes.txt, feature-x and origin are examples: swap in your own. Bookmark this page or download the free PDF.
Set up Git
| Command | What it does |
|---|---|
git --version |
Print the Git version |
git config --global user.name "Your Name" |
Set the name that goes on your commits |
git config --global user.email "you@example.com" |
Set the email that goes on your commits |
git config --global init.defaultBranch main |
Name the first branch of new repositories |
git config --list |
List all the settings Git can see |
Start a repository
| Command | What it does |
|---|---|
git init |
Create an empty Git repository in this folder |
git init -b main |
The same, with the initial branch named main |
git clone https://example.com/repo.git |
Clone a repository into a new folder |
git clone --depth 1 https://example.com/repo.git |
Shallow clone: the history is truncated to 1 commit |
git clone -b dev https://example.com/repo.git |
Clone and check out the branch dev instead of the remote default |
Stage and commit
| Command | What it does |
|---|---|
git status |
Show the working tree status |
git status -s |
The same in the short format |
git add notes.txt |
Add the contents of a file to the index (stage it) |
git add . |
Stage the changes in the current folder and below |
git add -p |
Interactively choose hunks of changes to stage |
git commit -m "Fix login bug" |
Record the staged changes with a message |
git commit -a -m "Fix login bug" |
Automatically stage modified and deleted files, then commit (new files are not affected) |
git commit --amend |
Replace the tip of the current branch with a new commit |
git commit --amend --no-edit |
Amend the last commit and keep its message |
Remember: git commit -a does not pick up new, untracked files. Use git add for those.
Look at changes and history
| Command | What it does |
|---|---|
git diff |
Show changes in the working tree not yet staged for the next commit |
git diff --staged |
Show the changes you staged for the next commit |
git log |
Show the commit logs |
git log --oneline |
One line per commit |
git log --oneline --graph --decorate --all |
A text graph of all refs, with branch and tag names |
git log -p |
Show the change each commit made |
git log --stat |
Show a summary of the files each commit changed |
git log --author="Alice" |
Only commits whose author matches the pattern |
git show |
Show the latest commit and what it changed |
git blame notes.txt |
Show which commit and author last changed each line of a file |
git grep "TODO" |
Print the lines that match a pattern |
Branches
| Command | What it does |
|---|---|
git branch |
List branches |
git branch --all |
List remote-tracking and local branches |
git branch feature-x |
Create a branch |
git branch -m new-name |
Rename the current branch |
git branch -d feature-x |
Delete a branch (it must be fully merged) |
git branch -D feature-x |
Delete a branch even if it is not merged |
git switch feature-x |
Switch to a branch |
git switch -c feature-x |
Create a branch and switch to it |
git checkout feature-x |
Also switches branches (git checkout also restores files) |
git checkout -b feature-x |
Also creates a branch and switches to it |
Remember: git switch only switches branches. git restore (below) handles files.
Merge and rebase
| Command | What it does |
|---|---|
git merge feature-x |
Join feature-x into the branch you are on |
git merge --no-ff feature-x |
Always create a merge commit, even if the history could be fast-forwarded |
git merge --abort |
Abort a merge in conflict and try to go back to how things were before it |
git rebase main |
Reapply your commits on top of main |
git rebase -i HEAD~3 |
Edit the list of the last 3 commits before reapplying them |
git rebase --continue |
Carry on after you resolved a conflict |
git rebase --abort |
Abort the rebase and go back to your original branch |
git cherry-pick abc1234 |
Apply the changes from an existing commit as a new commit |
Undo things
| Command | What it does |
|---|---|
git restore notes.txt |
Restore a file in the working tree from the index (the default source) |
git restore --staged notes.txt |
Unstage a file and keep your changes |
git reset --soft HEAD~1 |
Undo the last commit and leave the files and the index unchanged |
git reset HEAD~1 |
Undo the last commit and unstage its changes (the default mode is --mixed) |
git reset --hard HEAD~1 |
Undo the last commit and overwrite your files to match the earlier commit (uncommitted changes are lost) |
git revert abc1234 |
Record a new commit that reverses the effect of an earlier commit |
git rm notes.txt |
Remove a file from the working tree and from the index |
git rm --cached notes.txt |
Stop tracking a file but keep it on disk |
git mv old.txt new.txt |
Move or rename a file |
git clean -n |
Show which untracked files would be removed |
git clean -fd |
Remove untracked files and folders |
git reflog |
Show the reflog, which records when the tips of branches and other references were updated in your local repository |
Stash
| Command | What it does |
|---|---|
git stash |
Save your local changes and roll the files back to HEAD (the same as git stash push) |
git stash push -m "wip login" |
Stash with a description |
git stash -u |
Stash untracked files too |
git stash list |
List your stash entries |
git stash pop |
Apply the latest stash on top of your working tree and remove it from the list |
git stash apply |
Apply a stash and keep it in the list |
git stash drop |
Remove one stash entry from the list |
Remotes
| Command | What it does |
|---|---|
git remote -v |
List your remotes with their URLs |
git remote add origin https://example.com/repo.git |
Add a remote called origin |
git fetch |
Download objects and refs from a remote without merging |
git fetch --prune |
Fetch, and remove remote-tracking branches that no longer exist on the remote |
git pull |
Fetch from the remote and integrate the changes |
git pull --rebase |
Fetch, then rebase your branch on top of the upstream branch |
git push |
Update the remote with your commits |
git push -u origin main |
Push and set the upstream (tracking) reference for the branch |
git push --force-with-lease |
Force-push only if the remote ref is still at the value you expect |
git push origin --delete feature-x |
Delete a branch on the remote |
Ignore files with .gitignore
| Line in .gitignore | What it does |
|---|---|
*.log |
Ignore every file that ends in .log |
build/ |
Ignore a folder called build |
!keep.log |
Do not ignore keep.log, even though a pattern above matches it |
# comment |
A line that starts with # is a comment |
Remember: Put the lines in a file called .gitignore in your repository. It lists files you intentionally want Git to leave untracked.
Tags
| Command | What it does |
|---|---|
git tag |
List tags |
git tag v1.0 |
Create a lightweight tag on the current commit |
git tag -a v1.0 -m "Release 1.0" |
Create an annotated tag with a message |
git tag -d v1.0 |
Delete a tag |
git push origin v1.0 |
Push one tag to the remote |
git push --tags |
Push all tags |
Find a bug with bisect
| Command | What it does |
|---|---|
git bisect start |
Start a binary search for the commit that introduced a bug |
git bisect bad |
Mark the current commit as bad |
git bisect good v1.0 |
Mark a commit or tag as good |
git bisect reset |
Finish and return to where you started |
Remember: After bisect bad and bisect good, Git checks out a commit in the middle. Test it and mark it good or bad until it finds the first bad commit.
Common mistakes
-
git reset --hardoverwrites your files with the version from the commit you name, and tracked files that are not in that commit are removed. Commit or stash first if you want to keep your work. -
git commit -astages only files Git already tracks. A brand new file needsgit addfirst. -
git branch -Ddeletes a branch even when it is not merged.git branch -drefuses if it is not fully merged. -
git cleanremoves untracked files from the working tree. Rungit clean -nfirst to see what it would do. -
git revertmakes a new commit that reverses an old one.git resetmoves HEAD to a different commit. -
git stashleaves untracked files behind unless you add-u. -
git fetchdownloads but does not integrate.git pullfetches and then integrates the changes. -
git push --force-with-leaseoverwrites the remote ref only if it is still at the value you expect. Plain--forcedoes not check. - Git and GitHub are not the same thing. The GH-900 skills measured include explaining the difference between them.
Git FAQ
What is the difference between git fetch and git pull?git fetch downloads objects and refs from the remote without merging them. git pull fetches and then integrates the changes into your current branch.
What is the difference between git reset and git revert?git revert records a new commit that reverses the effect of an earlier commit. git reset sets HEAD (and, depending on the mode, the index and your files) to a different commit.
How do I unstage a file?
Run git restore --staged notes.txt. Your changes stay in the working tree.
How do I undo my last commit but keep the changes?
Run git reset --soft HEAD~1 to keep them staged, or git reset HEAD~1 to keep them unstaged.
What is the difference between git switch and git checkout?git switch only switches branches. git checkout can switch branches or restore working tree files.
Practise it
Git and GitHub basics, including key Git concepts such as repositories, commits and branches, are in the skills measured for the Microsoft GitHub Foundations (GH-900) exam. Try the free GitHub GH-900 practice test, and see our Linux commands cheat sheet.
Commands and options follow the Git reference manual on git-scm.com/docs (the git, config, init, clone, status, add, commit, diff, log, show, blame, grep, branch, switch, checkout, merge, rebase, cherry-pick, restore, reset, revert, rm, mv, clean, reflog, stash, remote, fetch, pull, push, tag and bisect pages, and gitignore), checked on 17 November 2026, when git-scm.com listed 2.56.0 as the latest source release. The GH-900 point comes from the Microsoft GH-900 study guide. Names, URLs and commit IDs are examples. Options change between Git versions, so check the manual for your installed version before relying on a flag.
Studying for GitHub Foundations? See the GitHub GH-900 practice questions.
What people say about this pack
Wonderful, the GH-900 exam dumps effectively simplified challenging topics, and I'm delighted with my 75% passing score. Thanks very much!
New version with real questions and accurate answers for GH-900 exam is great. Wonderful, I passed.