Git Cheat Sheet, Grouped by What You Want to Do

Most daily Git work is six commands: git status to see changes, git add to stage them, git commit -m to save, git switch -c to branch, git pull to get updates and git push to share. To undo, use git restore for files, git reset for local commits and git revert for commits already pushed.

Undo at a glance
Undo whatCommand
Edits to a filegit restore file
Staginggit restore --staged file
Last commit, not pushedgit reset --soft HEAD~1
A pushed commitgit revert <commit>

Updated

Search the commands below, copy any line with one click, and use the recipes when something has gone wrong. Commands use git switch and git restore, the clearer replacements for the many jobs of git checkout.

Writing a README for the repository? Use the Markdown cheat sheet.

Where changes live

Working folder (git add) Staging area (git commit) Local repository (git push) Remote (GitHub, GitLab). git pull brings the remote's commits back down.

Commands by task

Set up once
CommandWhat it does
git config --global user.name "Your Name"Name stamped on your commits
git config --global user.email "[email protected]"Email stamped on your commits
git config --global init.defaultBranch mainNew repositories start on main
git config --global pull.rebase truegit pull rebases instead of making merge commits
Start a project
CommandWhat it does
git init -b mainTurn the current folder into a repository
git clone https://github.com/org/repo.gitCopy a remote repository to your machine
git remote add origin https://github.com/org/repo.gitConnect a local repository to a remote
git remote -vList remotes and their URLs
Save work
CommandWhat it does
git statusWhat changed, what is staged
git add path/to/fileStage one file
git add -pStage parts of files, hunk by hunk
git commit -m "Add contact form"Commit what is staged
git diffChanges not yet staged
git diff --stagedChanges staged for the next commit
git log --oneline --graph --allCompact history with branches drawn
Branch and merge
CommandWhat it does
git switch -c feature/menuCreate a branch and move to it
git switch mainMove to an existing branch
git branchList local branches
git merge feature/menuMerge that branch into the current one
git rebase mainReplay the current branch on top of main
git branch -d feature/menuDelete a merged branch (-D forces)
git cherry-pick <commit>Copy one commit onto the current branch
Sync and deploy
CommandWhat it does
git fetchDownload remote changes without touching your files
git pullFetch, then merge or rebase into the current branch
git push -u origin feature/menuPush a new branch and remember where it goes
git pushPush the current branch
git tag -a v1.2.0 -m "Release 1.2.0"Mark a release
git push origin v1.2.0Push that tag (tags are not pushed by default)
Park work for later
CommandWhat it does
git stash push -m "half-done header"Put uncommitted changes aside
git stash listSee what is stashed
git stash popBring back the latest stash and remove it from the list

I messed up: recipes

I messed up: recipes
CommandWhat it does
git restore path/to/fileThrow away unstaged edits to a file (cannot be undone)
git restore --staged path/to/fileUnstage a file but keep the edits
git commit --amend -m "Better message"Fix the last commit's message (or add staged files to it) before pushing
git reset --soft HEAD~1Undo the last commit, keep its changes staged
git reset --hard HEAD~1Delete the last commit and its changes (local, not pushed)
git revert <commit>Undo a pushed commit with a new commit; safe on shared branches
git reflogFind a commit you lost after a reset or rebase, then git switch -c rescue <sha>
git merge --abortBack out of a merge with conflicts
git rebase --abortBack out of a rebase that went wrong
git switch -c new-branchCommitted to main by mistake: put the work on a branch, then reset main
git push --force-with-leaseOverwrite a remote branch only if nobody pushed since you last fetched
git clean -n then git clean -fdPreview, then delete untracked files and folders

npm commands

npm commands
CommandWhat it does
npm init -yCreate package.json with defaults
npm installInstall everything in package.json
npm ciClean install exactly from the lock file (use in CI)
npm install zodAdd a dependency
npm install -D vitestAdd a dev-only dependency
npm uninstall zodRemove a dependency
npm run buildRun a script from package.json
npm outdatedList packages with newer versions
npm updateUpdate within the ranges in package.json
npm auditReport known vulnerabilities
npm ls reactShow which versions of a package are installed and why
npm view react versionLatest published version of a package
npx create-vite@latest my-appRun a package's command without installing it

Step-by-step fixes

Committed to main instead of a branch

Make a branch where you are (it keeps the commits), then move main back to match the remote. Only do this before you push.

git switch -c feature/menu      # new branch keeps your commits
git switch main
git reset --hard origin/main    # main goes back to what is on the remote
git switch feature/menu

Resolving a merge conflict

Git stops and marks the clashing lines. Edit each file to the version you want, remove the markers, then stage and finish.

git merge feature/menu
# CONFLICT (content): Merge conflict in src/Header.tsx
# Edit the file: keep what you want between <<<<<<< ======= >>>>>>>, delete the markers
git add src/Header.tsx
git commit                      # finishes the merge
# Changed your mind? git merge --abort

Pushed a secret by mistake

Rotate the key first: once pushed, assume it is copied. Then remove it from the code and history, and add the file to .gitignore.

# 1. Revoke or rotate the key with its provider. Do this first.
# 2. Stop tracking the file and ignore it
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Stop tracking .env"
# 3. To purge it from history, use git filter-repo (a separate tool), then force-push

Where your changes live

Git keeps four places in mind: your working folder, the staging area (also called the index), your local repository, and the remote. add moves changes from the folder to staging, commit from staging into the repository, push from your repository to the remote, and pull brings the remote's commits back down.

From commit to a live website

Many hosts deploy when you push to a branch. Work on a feature branch, push it to get a preview, and merge into main to publish. Tag releases so you can return to a known good version quickly.

How it works

  1. Type a word such as undo, branch or tag into the search box.
  2. Copy the command and swap in your branch or file name.
  3. Check the diagram to see which area a command moves changes between.
  4. Run git status before and after anything that rewrites history.

Tips

Frequently asked questions

What is the difference between git reset and git revert?

reset moves your branch back, removing commits from its history; that is fine for local work. revert adds a new commit that undoes an old one, leaving history intact, which is the safe choice once a commit is pushed.

git switch or git checkout?

Both work. Git 2.23 added git switch for changing branches and git restore for restoring files, splitting the two jobs that git checkout used to do. They are easier to read and harder to misuse.

What does git fetch do that git pull does not?

fetch downloads new commits but changes nothing in your branch. pull runs fetch and then merges or rebases those commits into your current branch.

How do I get back a commit I lost?

Run git reflog. It lists every position your HEAD has been at, including commits no branch points to. Find the one you want and run git switch -c rescue <sha>.

npm install or npm ci?

Use npm install while developing; it can update the lock file. Use npm ci in builds and CI; it deletes node_modules and installs exactly what the lock file says, failing if package.json and the lock file disagree.

Related: Regex cheat sheet, Markdown cheat sheet, JSON formatter, App spec template, All free tools.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap