Git

Everyday Git commands and the handful of workflows that come up on every project: branching, merging and rebasing, syncing with a remote, undoing mistakes, and digging through history.

Start here

The Git commands you'll reach for most.

git status

Show the state of the working directory: staged, unstaged, and untracked changes — run this constantly

git add .

Stage every changed and new file in the current directory

git commit -m "message"

Save the staged changes as a new commit with the given message

git switch -c <branch>

Create a new branch and switch to it in one step — the everyday way to start work

git merge <branch>

Merge another branch into the current branch, creating a merge commit if needed

git push

Upload local commits on the current branch to its remote tracking branch

git pull

Fetch from a remote and immediately merge (or rebase, if configured) into the current branch

git restore <file>

Discard unstaged changes in a file, reverting it to the last commit

git stash

Temporarily shelve uncommitted changes so you can switch context, then come back to them

Every Git command at a glance

Every entry, one line each. Highlighted ones have a detailed example below — click to jump there.

All 79 entries
git config --global user.name "Name"
Set the name attached to your commits, machine-wide — do this first
git config --global user.email "you@example.com"
Set the email attached to your commits, machine-wide
git init
Turn the current directory into a new, empty Git repository
git clone <url>
Copy an existing remote repository (and its full history) to your machine
.gitignore
A file listing paths Git should never track (build output, secrets, node_modules, …)
git help <command>
Open the manual page for a specific Git command
git config --list
Show every active config setting and where it came from
git config --global init.defaultBranch main
Set the default branch name used by new repos
git config --global alias.co checkout
Create a shortcut, e.g. `git co` instead of `git checkout`
Working directory → Staging area → Repository
Git's three areas: edit files in the working directory, `git add` promotes changes to the staging area (the index), `git commit` saves that staged snapshot into the repository's history
git status
Show the state of the working directory: staged, unstaged, and untracked changes — run this constantly
git add <file>
Stage a file — mark it to be included in the next commit
git add .
Stage every changed and new file in the current directory
git diff
Show unstaged changes — what differs between the working directory and the staging area
git diff --staged
Show staged changes — what differs between the staging area and the last commit
git commit -m "message"
Save the staged changes as a new commit with the given message
git commit -am "message"
Stage every already-tracked file and commit in one step (skips new, untracked files)
git commit --amend
Rewrite the most recent commit — change its message or fold in new staged changes
git log
Show the commit history for the current branch
git log --oneline --graph --all
A compact, visual one-line-per-commit history across every branch
git show <commit>
Show the full details and diff of a single commit
git add -p
Interactively stage specific hunks within a file instead of the whole thing
git rm <file>
Delete a file and stage that deletion
git mv <old> <new>
Rename or move a tracked file and stage the change
HEAD
A pointer to the commit your working directory is currently checked out at — normally the tip of your current branch
git branch
List local branches; the current one is marked with an asterisk
git switch -c <branch>
Create a new branch and switch to it in one step — the everyday way to start work
git switch <branch>
Switch the working directory to another existing branch
git branch -m <new-name>
Rename the current branch
git branch -d <name>
Delete a branch (refuses if it has unmerged commits)
git branch -D <name>
Force-delete a branch, even if unmerged
git checkout -b <branch>
The older equivalent of `git switch -c` — create and switch in one step
git checkout <branch>
The older command for switching branches (also used for restoring files)
git merge <branch>
Merge another branch into the current branch, creating a merge commit if needed
git merge --no-ff <branch>
Force a merge commit even when a fast-forward would be possible, preserving branch history
Resolving a merge conflict
Edit the conflicted file to remove the `<<<<<<<`/`=======`/`>>>>>>>` markers, keep what you want, then `git add` and commit (or `git rebase --continue`)
git rebase <branch>
Replay the current branch's commits on top of another branch, producing linear history instead of a merge commit
git rebase -i <commit>
Interactively reorder, squash, edit, or drop commits since the given commit
git rebase --abort
Cancel an in-progress rebase and return to the state before it started
git cherry-pick <commit>
Apply a single commit from another branch onto the current branch
git remote -v
List configured remotes and their URLs
git remote add origin <url>
Register a remote repository under the name "origin"
git push -u origin <branch>
Push a branch and set it to track that remote branch, so plain `git push`/`git pull` work afterward
git push
Upload local commits on the current branch to its remote tracking branch
git pull
Fetch from a remote and immediately merge (or rebase, if configured) into the current branch
git fetch
Download new commits and branches from a remote without merging them — the safe way to "look before you pull"
git pull --rebase
Fetch and replay local commits on top of the updated remote branch instead of merging
git push --force-with-lease
Force-push, but abort if the remote has commits you haven't seen yet — far safer than --force
git push origin --delete <branch>
Delete a branch on the remote
Sync a fork with upstream
git remote add upstream <url>, then git fetch upstream && git merge upstream/main
git restore <file>
Discard unstaged changes in a file, reverting it to the last commit
git restore --staged <file>
Unstage a file without touching its contents
git reset --soft HEAD~1
Undo the last commit but keep its changes staged, ready to re-commit differently (forgot something? git commit --amend above is quicker)
git reset --mixed <commit>
Move the branch pointer to a commit, keeping changes but unstaging them (the default mode)
git reset --hard <commit>
Move the branch pointer to a commit and discard all changes since then — destructive
git revert <commit>
Create a new commit that undoes the changes of a previous commit, without rewriting history — safe on shared branches
git stash
Temporarily shelve uncommitted changes so you can switch context, then come back to them
git stash pop
Reapply the most recent stash and remove it from the stash list
git stash list
List all stashed changesets
git clean -fd
Permanently delete untracked files and directories — destructive, run with -n first to preview
git reflog
Show a log of everywhere HEAD has pointed — the ultimate safety net for recovering commits that seem 'lost'
fatal: not a git repository
You're not inside a Git repo (or a parent folder) — run git init, or cd into the right directory
Merge conflict
Two branches changed the same lines — Git pauses the merge/rebase and asks you to resolve the conflicted file(s) by hand (see Merging & Rebasing above)
Detached HEAD state
HEAD points directly at a commit instead of a branch — commits made here can be lost; run git switch -c <name> to keep them
Updates were rejected (non-fast-forward)
The remote has commits you don't have locally — run git pull (or git fetch + rebase/merge) before pushing again
fatal: refusing to merge unrelated histories
You're merging two branches with no common commit — add --allow-unrelated-histories if that's actually intended
Your branch is ahead/behind by N commits
Shown by git status — "ahead" means commits to push, "behind" means commits to pull
git log -p <file>
Show the full diff of every commit that touched a file
git diff main..feature
See everything that changed on one branch but not another
git blame <file>
Show who last changed each line of a file, and in which commit
git log --follow <file>
Show a file's history across renames
git bisect start
Begin a binary search through commit history to find the exact commit that introduced a bug
git tag <name>
Mark the current commit with a readable label, often used for releases
git tag -a <name> -m "message"
Create an annotated tag with a message, author, and date
git describe
Describe the current commit in terms of the nearest reachable tag
Feature branch workflow
Branch off main for each feature/fix, open a PR, merge back in, delete the branch
Discard all local changes
git restore . (unstaged) and git restore --staged . (staged), or git reset --hard for everything at once
Squash the last N commits
git rebase -i HEAD~N, then mark all but the first as "squash"

Worked examples with live previews, for the parts worth seeing in action.

Everyday Basics

Fixing the Last Commit

git add forgotten-file.ts
git commit --amend --no-edit

--amend replaces the previous commit with a new one that includes whatever is currently staged. --no-edit keeps the original commit message; drop it to edit the message instead. Only amend commits that haven’t been pushed yet — amending a shared commit rewrites history others already have.

Change the message or add forgotten changes without creating a new commit.

  • amend
  • commit
  • fix

Merging & Rebasing

Interactive Rebase: Squashing Commits

git rebase -i HEAD~3

Opens an editor listing the last 3 commits, oldest first. Change pick to squash (or s) on every line after the first, save, then write the combined commit message. The three commits become one. Never rebase commits that have already been pushed and shared with others.

Collapse a messy string of WIP commits into one clean commit before merging.

  • rebase
  • squash
  • interactive
  • history

Resolving a Merge Conflict

<<<<<<< HEAD
your current branch's version
=======
the incoming branch's version
>>>>>>> feature-branch

Edit the file to keep the lines you want and delete the <<<<<<<, =======, and >>>>>>> markers entirely. Then git add <file> to mark it resolved, and git commit (or git rebase --continue if you’re mid-rebase) to finish.

What those conflict markers mean and the steps to clear them.

  • merge
  • conflict
  • rebase
  • workflow

Undoing Changes

Stashing Work in Progress

git stash              # shelve tracked changes
git switch main        # go do something else
git switch -           # come back to your branch
git stash pop          # reapply the shelved changes and drop the stash

Add -u to git stash to also shelve untracked files. Use git stash list to see every stash you’ve got saved, and git stash apply instead of pop if you want to keep the stash around after reapplying it.

Shelve uncommitted changes to switch branches, then bring them back.

  • stash
  • wip
  • switch

Inspecting History

Finding a Bug with Bisect

git bisect start
git bisect bad                 # current commit is broken
git bisect good v1.2.0         # this old tag was fine
# Git checks out a commit halfway between — test it, then:
git bisect good   # or
git bisect bad
# repeat until Git names the exact commit that introduced the bug
git bisect reset               # return to your original branch

Each good/bad answer halves the remaining range, so even thousands of commits resolve in a dozen or so steps.

Binary-search commit history to find exactly which commit broke something.

  • bisect
  • debug
  • history
webcheet

↑↓ navigate↵ openesc close