Skip to content

Git

A quick reference for everyday Git commands.


Start a new repo:

Terminal window
git init

Clone an existing repo:

Terminal window
git clone <url>

Add an untracked file or unstaged changes:

Terminal window
git add <file>

Add all untracked files and unstaged changes:

Terminal window
git add .

Choose which parts of a file to stage:

Terminal window
git add -p

Move a file:

Terminal window
git mv <old> <new>

Delete a file:

Terminal window
git rm <file>

Tell Git to forget about a file without deleting it:

Terminal window
git rm --cached <file>

Unstage one file:

Terminal window
git reset <file>
# OR
git restore --staged <file>

Unstage everything:

Terminal window
git reset
# OR
git restore --staged .

Check what you added:

Terminal window
git status

Make a commit (and open a text editor to write the message):

Terminal window
git commit

Make a commit:

Terminal window
git commit -m 'message'

Commit all unstaged changes:

Terminal window
git commit -am 'message'

Switch branches:

Terminal window
git switch <name>
# OR
git checkout <name>

Create a branch:

Terminal window
git switch -c <name>
# OR
git checkout -b <name>

List branches:

Terminal window
git branch

List branches by most recently committed to:

Terminal window
git branch --sort=-committerdate

Delete a branch:

Terminal window
git branch -d <name>

Force delete a branch:

Terminal window
git branch -D <name>

Diff all staged and unstaged changes:

Terminal window
git diff HEAD

Diff just staged changes:

Terminal window
git diff --staged

Diff just unstaged changes:

Terminal window
git diff

Show the diff between a commit and its parent:

Terminal window
git show <commit>

Diff two commits:

Terminal window
git diff <commit> <commit>

Diff one file since a commit:

Terminal window
git diff <commit> <file>

Show a summary of a diff:

Terminal window
git diff <commit> --stat
git show <commit> --stat

Every time we say <commit>, you can use any of these:

Reference Example
★ a branch main
★ a tag v0.1
★ a commit ID 3e887ab
★ a remote branch origin/main
★ current commit HEAD
★ 3 commits ago HEAD^^^ or HEAD~3

Delete unstaged changes to one file:

Terminal window
git restore <file>
# OR
git checkout <file>

Delete all staged and unstaged changes to one file:

Terminal window
git restore --staged --worktree <file>
# OR
git checkout HEAD <file>

Delete all staged and unstaged changes:

Terminal window
git reset --hard
# OR
git restore --staged --worktree .

Delete untracked files:

Terminal window
git clean

‘Stash’ all staged and unstaged changes:

Terminal window
git stash

“Undo” the most recent commit (keep your working directory the same):

Terminal window
git reset HEAD^

Squash the last 5 commits into one:

Terminal window
git rebase -i HEAD~5

Then change pick to fixup for any commit you want to combine with the previous one.

Undo a failed rebase:

Terminal window
git reflog BRANCHNAME

Then manually find the right commit ID in the reflog, then run:

Terminal window
git reset --hard <commit>

Change a commit message (or add a file you forgot):

Terminal window
git commit --amend

Look at a branch’s history:

Terminal window
git log main
git log --graph main
git log --oneline

Show every commit that modified a file:

Terminal window
git log <file>

Show every commit that modified a file, including before it was renamed:

Terminal window
git log --follow <file>

Find every commit that added or removed some text:

Terminal window
git log -G banana

Show who last changed each line of a file:

Terminal window
git blame <file>

Every example below starts from the same history: main has commits A B C, and banana branched off after B with commits D E.

Terminal window
git switch banana
git rebase main
Before:
D---E banana
/
A---B---C main
After:
D'---E' banana
/
A---B---C---+ main
\
D---E (original commits are "lost")
Terminal window
git switch main
git merge banana
Before:
D---E banana
/
A---B---C main
After:
D---E banana
/ \
A---B---C---◇ main
Terminal window
git switch main
git merge --squash banana
git commit
Before:
D---E banana
/
A---B---C main
After:
D---E banana
/
A---B---C---DE main (D and E squashed into one commit)

Bring a branch up to date with another branch (a.k.a. “fast-forward merge”)

Section titled “Bring a branch up to date with another branch (a.k.a. “fast-forward merge”)”
Terminal window
git switch main
git merge banana
Before:
A---B---C main
\
D---E banana
After:
A---B---C---D---E main, banana

Terminal window
git cherry-pick <commit>
Before:
D---E
/
A---B---C main
After:
D---E
/
A---B---C---D' main

Get the version of a file from another commit:

Terminal window
git checkout <commit> <file>
# OR
git restore <file> --source <commit>

Terminal window
git remote add <name> <url>

Push the main branch to the remote origin:

Terminal window
git push origin main

Push the current branch to its remote “tracking branch”:

Terminal window
git push

Push a branch that you’ve never pushed before:

Terminal window
git push -u origin <name>

Force push:

Terminal window
git push --force-with-lease

Push tags:

Terminal window
git push --tags

Fetch changes (but don’t change any of your local branches):

Terminal window
git fetch origin main

Fetch changes and then rebase your current branch:

Terminal window
git pull --rebase

Fetch changes and then merge them into your current branch:

Terminal window
git pull origin main
# OR
git pull

Set a config option:

Terminal window
git config user.name 'Your Name'

Set an option globally:

Terminal window
git config --global ...

Add an alias:

Terminal window
git config alias.st status

See all possible config options:

Terminal window
man git-config

Purpose Location
Local git config .git/config
Global git config ~/.gitconfig
List of files to ignore .gitignore