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 what | Command |
|---|---|
| Edits to a file | git restore file |
| Staging | git restore --staged file |
| Last commit, not pushed | git reset --soft HEAD~1 |
| A pushed commit | git 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
| Command | What 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 main | New repositories start on main |
| git config --global pull.rebase true | git pull rebases instead of making merge commits |
| Command | What it does |
|---|---|
| git init -b main | Turn the current folder into a repository |
| git clone https://github.com/org/repo.git | Copy a remote repository to your machine |
| git remote add origin https://github.com/org/repo.git | Connect a local repository to a remote |
| git remote -v | List remotes and their URLs |
| Command | What it does |
|---|---|
| git status | What changed, what is staged |
| git add path/to/file | Stage one file |
| git add -p | Stage parts of files, hunk by hunk |
| git commit -m "Add contact form" | Commit what is staged |
| git diff | Changes not yet staged |
| git diff --staged | Changes staged for the next commit |
| git log --oneline --graph --all | Compact history with branches drawn |
| Command | What it does |
|---|---|
| git switch -c feature/menu | Create a branch and move to it |
| git switch main | Move to an existing branch |
| git branch | List local branches |
| git merge feature/menu | Merge that branch into the current one |
| git rebase main | Replay the current branch on top of main |
| git branch -d feature/menu | Delete a merged branch (-D forces) |
| git cherry-pick <commit> | Copy one commit onto the current branch |
| Command | What it does |
|---|---|
| git fetch | Download remote changes without touching your files |
| git pull | Fetch, then merge or rebase into the current branch |
| git push -u origin feature/menu | Push a new branch and remember where it goes |
| git push | Push the current branch |
| git tag -a v1.2.0 -m "Release 1.2.0" | Mark a release |
| git push origin v1.2.0 | Push that tag (tags are not pushed by default) |
| Command | What it does |
|---|---|
| git stash push -m "half-done header" | Put uncommitted changes aside |
| git stash list | See what is stashed |
| git stash pop | Bring back the latest stash and remove it from the list |
I messed up: recipes
| Command | What it does |
|---|---|
| git restore path/to/file | Throw away unstaged edits to a file (cannot be undone) |
| git restore --staged path/to/file | Unstage 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~1 | Undo the last commit, keep its changes staged |
| git reset --hard HEAD~1 | Delete 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 reflog | Find a commit you lost after a reset or rebase, then git switch -c rescue <sha> |
| git merge --abort | Back out of a merge with conflicts |
| git rebase --abort | Back out of a rebase that went wrong |
| git switch -c new-branch | Committed to main by mistake: put the work on a branch, then reset main |
| git push --force-with-lease | Overwrite a remote branch only if nobody pushed since you last fetched |
| git clean -n then git clean -fd | Preview, then delete untracked files and folders |
npm commands
| Command | What it does |
|---|---|
| npm init -y | Create package.json with defaults |
| npm install | Install everything in package.json |
| npm ci | Clean install exactly from the lock file (use in CI) |
| npm install zod | Add a dependency |
| npm install -D vitest | Add a dev-only dependency |
| npm uninstall zod | Remove a dependency |
| npm run build | Run a script from package.json |
| npm outdated | List packages with newer versions |
| npm update | Update within the ranges in package.json |
| npm audit | Report known vulnerabilities |
| npm ls react | Show which versions of a package are installed and why |
| npm view react version | Latest published version of a package |
| npx create-vite@latest my-app | Run 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/menuResolving 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 --abortPushed 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
- Type a word such as undo, branch or tag into the search box.
- Copy the command and swap in your branch or file name.
- Check the diagram to see which area a command moves changes between.
- Run git status before and after anything that rewrites history.
Tips
- Commit small and often; small commits are easy to revert and review.
- Never reset or force-push a branch other people use. Revert instead.
- Prefer --force-with-lease over --force; it refuses if someone else pushed.
- Write commit messages as what the change does: "Add booking form", not "changes".
- Keep secrets in environment files listed in .gitignore from the very first commit.
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.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- Git Rebase: Rebase vs Merge, Interactive Rebase and Undo
- How to Rename a Git Branch (Local and Remote)
- Git Worktree: Several Branches Checked Out at Once
- GitHub Personal Access Tokens for Git
- GitHub Webhooks: Create, Verify and Redeliver
- GitLab Webhooks: Setup, Verification and Failures
- Google Ads Conversion Tracking
- Google Ads Landing Page Experience
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App