Git Cheat Sheet

Setup, staging, branching, merging, remotes, stash, rebase, and history surgery — the version control core of every pipeline.

Core DevOps9 sections · 84 commands
⌘K

Setup & Config

9
git init

Initialize a repository

git config --global user.name "NAME"

Set the commit author name

git config --global user.email "USER@EMAIL"

Set the commit author email

git config --global core.editor code

Set the default editor

git config --list

Show effective configuration

git config --global init.defaultBranch main

Default branch to main

git clone URL DIR

Clone a repository

git clone --depth 1 URL

Shallow clone (faster)

git config alias.co checkout

Create a shortcut alias

Staging & Committing

16
git status

Show working tree state

git diff

Unstaged changes

git diff --staged

Staged changes

git add FILE

Stage a file

git add -p

Stage hunks interactively

git add -A

Stage all changes (incl. deletions)

git rm FILE

Remove and stage the deletion

git mv OLD NEW

Rename and stage

git commit -m "message"

Commit staged changes

git commit -am "message"

Stage tracked changes and commit

git commit --amend -m "new msg"

Redo the last commit message

git commit --amend --no-edit

Fold staged changes into the last commit

git restore FILE

Discard unstaged changes to a file

git restore --staged FILE

Unstage a file (keep changes)

git reset HEAD~1

Undo the last commit, keep changes

git reset --hard HEAD~1

Undo the last commit, discard changes

Branching & Merging

12
git branch

List local branches

git branch NAME

Create a branch

git checkout -b NAME

Create and switch to a branch

git switch -c NAME

Modern create-and-switch

git switch -

Switch to the previous branch

git merge NAME

Merge a branch into the current one

git merge --no-ff NAME

Merge with a merge commit

git branch -d NAME

Delete a merged branch

git branch -D NAME

Force-delete an unmerged branch

git branch --merged

Branches already merged

git branch --track NAME ORIGIN/NAME

Create a branch tracking upstream

git remote prune origin

Remove stale remote branches

Remotes & Syncing

9
git remote -v

List configured remotes

git remote add origin URL

Add an origin remote

git push origin main

Push the main branch

git push -u origin NAME

Push and set upstream

git push --force-with-lease

Force-push safely after rebase

git fetch

Download remote refs without merging

git pull --rebase

Pull and rebase local commits

git push origin --delete NAME

Delete a remote branch

git fetch origin PR_REF:NAME

Fetch a PR into a local branch

History & Diffing

10
git log --oneline

Compact commit list

git log --oneline --graph --all

Visual graph of all branches

git log -p FILE

Full diff history of a file

git log --since='2 weeks ago' --author=NAME

Filter commits by time and author

git show COMMIT

Show a commit's diff

git show HEAD:FILE

Show a file as of HEAD

git blame FILE

Who changed each line

git diff HEAD -- FILE

Working tree vs HEAD for a file

git diff BRANCH1..BRANCH2

Compare two branches

git shortlog -sn

Commit counts per author

Stash

8
git stash

Save uncommitted work aside

git stash -u

Also stash untracked files

git stash list

List stashes

git stash pop

Reapply the newest stash and drop it

git stash apply stash@{0}

Reapply a stash without dropping

git stash push -m "WIP"

Stash with a message

git stash branch NAME

Turn a stash into a branch

git stash drop stash@{1}

Drop a specific stash

Undo & Surgery

10
git revert COMMIT

Undo a commit with a new commit

git reset --soft HEAD~1

Undo commit, keep changes staged

git reset --mixed HEAD~1

Undo commit, keep changes unstaged

git reset --hard ORIG_HEAD

Undo a bad reset

git rebase -i HEAD~3

Interactive rebase of the last 3 commits

git rebase --abort

Cancel an in-progress rebase

git rebase --continue

Continue after resolving conflicts

git cherry-pick COMMIT

Apply a specific commit onto HEAD

git reflog

Every HEAD movement (recovery)

git gc --prune=now

Garbage-collect loose objects

Conflicts

6
git status

Files with conflict markers

git diff --name-only --diff-filter=U

List conflicted files

git checkout --ours FILE

Take the merge-side version

git checkout --theirs FILE

Take the incoming version

git mergetool

Launch the configured merge tool

git config merge.conflictStyle diff3

Show the base version too

Submodules

4
git submodule add URL PATH

Add a nested repository

git submodule update --init --recursive

Clone and sync all submodules

git submodule foreach git pull

Update every submodule

git submodule update --remote

Pull latest of every submodule