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.
Working folder (git add) Staging area (git commit) Local repository (git push) Remote (GitHub, GitLab). git pull brings the remote's commits back down.
| 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 |
| 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 |
| 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 |
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/menuGit 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 --abortRotate 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
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.
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.
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.
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.
fetch downloads new commits but changes nothing in your branch. pull runs fetch and then merges or rebases those commits into your current branch.
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>.
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.
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.