Cheat sheet

Git commands: an everyday cheat sheet with examples

Updated: · Skillaria.Top

The core Git commands are git init and git clone to create a repository, git status, git add and git commit to save changes, git branch, git switch and git merge to work with branches, git push, git fetch and git pull to sync with the server, plus git restore, git reset and git revert to undo changes.

Below is a practical Git cheat sheet: commands are grouped by task, each with an example and a short explanation. It's handy to keep open next to your terminal: nearly everything a developer does with Git in a day fits into 20–30 commands from these tables.

To make sense of the commands, keep three areas in mind: the working directory (files on disk), the index or staging area (what goes into the next commit), and the repository (the commit history in the .git folder). Most Git commands move changes between these three areas or between your local and a remote repository.

Setting up Git

You run these once after installing. The --global flag writes settings to ~/.gitconfig and applies them to all of your repositories; without it, they apply to the current repository only.

CommandWhat it does
git config --global user.name "Jane Smith"Commit author name
git config --global user.email "jane@example.com"Commit author email
git config --global init.defaultBranch mainDefault branch name for new repositories
git config --global core.editor "code --wait"Editor for commit messages (VS Code here)
git config --global pull.rebase falseDefault git pull behavior — merge
git config --global core.autocrlf inputConvert CRLF to LF on commit (macOS/Linux; usually true on Windows)
git config --list --show-originShow all settings and the file each one comes from
git config --global alias.st statusAlias: git st instead of git status
git --versionInstalled Git version
git help commitFull manual for a command (or git commit -h for a short version)

Creating and cloning a repository

CommandWhat it does
git initCreates an empty repository (a .git folder) in the current directory
git init my-projectCreates a my-project directory with a repository in it
git clone https://github.com/user/repo.gitCopies a remote repository with its full history
git clone git@github.com:user/repo.git appClones over SSH into the app folder
git clone --depth 1 URLShallow clone: only the latest commit, fast and small
git clone -b develop URLClones and checks out the develop branch right away

The basic loop: status, add, commit

This is the core of daily work: see what changed, stage what you need, and record it in a commit.

CommandWhat it does
git statusState of things: modified, staged and untracked files
git status -sShort format: M — modified, A — added, ?? — untracked
git add file.txtStage a file
git add .Stage all changes in the current directory and below, including deletions
git add -AStage all changes in the entire repository
git add -pInteractively pick individual chunks (hunks) of changes
git commit -m "Add login form"Create a commit from the index with a message
git commit -am "Fix typo"Stage all tracked modified files and commit; new files are not included
git commit --amendRewrite the last commit: add something you forgot to stage or fix the message
git commit --amend --no-editAdd to the last commit without changing the message
git rm file.txtDelete a file from disk and from the index
git rm --cached secret.envStop tracking a file while keeping it on disk
git mv old.js new.jsRename or move a file

A typical workflow looks like this:

git status
git add src/login.js src/login.css
git diff --staged          # check exactly what goes into the commit
git commit -m "Add login form"

Don't use --amend on commits you've already pushed to a shared branch: it creates a new commit with a different hash, and your teammates will have to deal with diverged history.

Viewing history and changes

CommandWhat it does
git logCommit history of the current branch
git log --onelineOne line per commit: short hash and subject
git log --oneline --graph --allGraph of all branches in the terminal
git log -p file.txtHistory of a file with the diff of each commit
git log --author="Jane" --since="2 weeks ago"Filter by author and date
git log -S "getUser"Commits that added or removed the string getUser
git log main..featureCommits that are in feature but not in main
git diffChanges in the working directory that aren't staged yet
git diff --stagedStaged changes compared to the last commit (same as --cached)
git diff main featureDifference between the tips of two branches
git diff HEAD~3 -- src/Changes in the src folder over the last three commits
git show a1b2c3dMetadata and diff of a specific commit
git show HEAD:README.mdContents of a file in the latest commit
git blame app.jsWho last changed each line, and in which commit
git shortlog -snNumber of commits per author

Commit references come in handy everywhere: HEAD is the current commit, HEAD~1 is its parent, HEAD~3 is three steps back along the first parent, and HEAD^2 is the second parent of a merge commit.

Branches: create, switch, delete

Since version 2.23, Git has git switch for branches and git restore for files — they split up the overloaded git checkout. The old forms still work.

CommandWhat it does
git branchList local branches; the current one is marked with *
git branch -aLocal and remote-tracking branches
git branch -vvBranches with their latest commit and ahead/behind status vs. upstream
git branch feature/loginCreate a branch without switching to it
git switch feature/loginSwitch to a branch
git switch -c feature/loginCreate a branch and switch to it (same as git checkout -b)
git switch -Go back to the previous branch
git branch -m new-nameRename the current branch
git branch -d feature/loginDelete a branch if it's already merged
git branch -D feature/loginForce-delete a branch, even if unmerged
git branch --mergedBranches already merged into the current one — candidates for deletion

Merging and rebasing

CommandWhat it does
git merge featureMerge feature into the current branch (fast-forward if possible)
git merge --no-ff featureAlways create a merge commit
git merge --squash featureStage all of the branch's changes as a single set, then run git commit
git merge --abortAbort a conflicted merge and return to the original state
git rebase mainReplay the current branch's commits on top of main
git rebase -i HEAD~4Interactively squash, reword, reorder or drop the last 4 commits
git rebase --continueContinue after resolving a conflict
git rebase --abortCancel the rebase entirely
git cherry-pick a1b2c3dApply a single commit to the current branch as a new one

Resolving a merge conflict almost always looks the same:

git merge feature
# CONFLICT (content): Merge conflict in app.js
git status                 # files listed under "both modified"
# edit app.js, remove the <<<<<<< ======= >>>>>>> markers
git add app.js
git commit                 # Git suggests a ready-made merge commit message

The golden rule of rebase: don't rebase commits that are already on a shared branch where others are building on them.

Working with remote repositories

CommandWhat it does
git remote -vList remotes and their URLs
git remote add origin git@github.com:user/repo.gitAdd a remote repository named origin
git remote set-url origin NEW_URLChange a remote's URL (for example, HTTPS to SSH)
git remote remove upstreamRemove a remote
git fetchDownload new commits and branches without touching your files
git fetch --pruneSame, plus remove references to branches deleted on the server
git pullgit fetch + merge the upstream branch into the current one
git pull --rebasegit fetch + rebase your commits on top of the fetched ones
git pushPush the current branch's commits to its upstream
git push -u origin feature/loginFirst push of a branch, setting its upstream
git push --force-with-leaseOverwrite the remote branch only if nobody else has pushed new commits to it
git push origin --delete feature/loginDelete a branch on the server
git push --tagsPush all local tags

Instead of git push --force, get used to --force-with-lease: if a teammate has pushed to the same branch in the meantime, the command refuses to overwrite their work.

Undoing changes

The most nerve-racking part of Git. The key question before picking a command: are the changes only in your files, already staged, in a local commit, or in a published commit?

SituationCommand
Discard edits to a file (restore it to the last commit)git restore app.js
Unstage a file while keeping the editsgit restore --staged app.js
Restore a file from a specific commitgit restore --source=a1b2c3d app.js
Undo the last commit, keeping the changes stagedgit reset --soft HEAD~1
Undo the last commit, keeping the changes in your filesgit reset HEAD~1 (default --mixed mode)
Delete the last commit and all its changesgit reset --hard HEAD~1
Undo a published commit with a new "inverse" commitgit revert a1b2c3d
Undo a merge commitgit revert -m 1 <merge-hash>
Preview which untracked files will be deletedgit clean -n
Delete untracked files and foldersgit clean -fd
Find a "lost" commit after a reset or rebasegit reflog, then git reset --hard HEAD@{2}

git reset --hard and git clean -f permanently delete uncommitted changes: the reflog only keeps commits, not edits in the working directory. Run git stash first, or at least git status.

Stash: set unfinished work aside

CommandWhat it does
git stashStash changes to tracked files and clean the working directory
git stash push -u -m "wip: form"Stash including untracked files, with a label
git stash listList stashes: stash@{0}, stash@{1}…
git stash show -p stash@{0}Show the diff of a stash
git stash popRestore the latest stash and remove it from the list
git stash apply stash@{1}Restore a stash, keeping it in the list
git stash drop stash@{1}Delete a single stash
git stash branch fix-formCreate a branch from the commit where the stash was made and apply it

Tags and releases

CommandWhat it does
git tagList tags
git tag -a v1.2.0 -m "Release 1.2.0"Annotated tag with author, date and message
git tag v1.2.0 a1b2c3dLightweight tag on a specific commit
git push origin v1.2.0Push a single tag
git tag -d v1.2.0Delete a tag locally
git push origin --delete v1.2.0Delete a tag on the server
git describe --tagsNearest tag and the number of commits since it, e.g. v1.2.0-3-ga1b2c3d

Advanced Git commands

CommandWhat it does
git bisect start, git bisect bad, git bisect good v1.1.0Binary search for the commit that introduced a bug; finish with git bisect reset
git worktree add ../hotfix mainA second working folder with another branch from the same repository
git submodule update --init --recursiveFetch submodules after cloning
git grep "TODO"Fast search through tracked files
git format-patch -1 HEAD / git apply fix.patchSave a commit as a patch and apply a patch to the working directory
git gcPack objects and clean up the repository (Git usually does this itself)

Common mistakes and how to fix them

Committed to main instead of a new branch

If the commit hasn't been pushed yet, create a branch at the current commit and roll main back:

git branch feature/login      # the branch points to your commit
git reset --hard HEAD~1       # main goes back one step
git switch feature/login

Forgot to add a file to the last commit

git add forgotten.js
git commit --amend --no-edit

Push rejected: non-fast-forward

The server has commits you don't. Pull them first, then push:

git pull --rebase
git push

Accidentally committed a file with passwords

Stop tracking it and add it to .gitignore: git rm --cached .env, then commit. If the commit has already been pushed, consider the secret compromised and rotate it — it's still in the history.

Summary

A short set covers day-to-day work: status, add, commit, switch, merge, pull, push and log --oneline. Choose among the undo commands — restore, reset, revert — based on whether the commit has been published, and remember reflog as your last-resort safety net. All the Git commands in this cheat sheet are covered hands-on in the Git course, where you try each one in your own repository.

FAQ

How many Git commands does a beginner need to know?

10–12 are enough to get started: init, clone, status, add, commit, log, diff, branch, switch, merge, pull and push. You'll pick up the rest as tasks come up.

What's the difference between git switch and git checkout?

git switch only switches and creates branches, while git checkout also restores files. The split into switch and restore was made so you don't accidentally wipe out edits by confusing a branch with a file.

How do I get help for any Git command?

Run git help command-name for the full manual or git command-name -h for a short list of flags. The same documentation is available on git-scm.com.

Can I shorten long Git commands?

Yes, with aliases: git config --global alias.lg "log --oneline --graph --all" lets you type git lg.

Do these commands work on Windows?

Yes, the commands are the same on every system. On Windows, you can run them in Git Bash, PowerShell or your code editor's terminal.