Post

Git cheat sheet

A note on the key Git commands.

Git cheat sheet

Introduction

This is not a comprehensive guide to Git commands, but a notebook I made for myself to quickly find the ones I have not used in a while and may have forgotten. For a comprehensive and up-to-date reference, check out the Git docs1 or the Git Pro book2.

Use the table of contents to quickly jump to the topic you’re interested in.

Setup and configuration

Install Git

Git installer

Get the installer from the official website3.

WinGet4

1
winget install --id Git.Git

Update Git

WinGet4.

1
winget update --id Git.Git

Git

1
git update-git-for-windows

Check Git version

1
git --version

Configure Git

View all configurations

1
2
3
4
5
6
7
8
# List all variables
git config list

# List variables from current repository only
git config list --local

# List global variables
git config list --global

View and set specific configurations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# View username and email configured globally
git config --global user.name
git config --global user.email

# Set username and email globally
git config --global user.name "first last"
git config --global user.email "user@gmail.com"

# View username and email for current repository
git config user.name
git config user.email

# Set  username and email for current repository
git config user.name "first last"
git config user.email "user@gmail.com"

See related recipe on how to Update user email after committing the wrong one.

Manage repository

Create repository

1
git init .

Clone a repository

1
git clone https://github.com/kungfux/project.git

Make changes

Add files to staging

1
2
3
4
5
6
7
8
9
10
11
# Add all changes
git add .

# Add files by mask
git add *.cs

# Add ignored file
git add -f ignored.file

# Add new filepath only
git add -N new.file

Commit changes

1
2
3
4
5
6
7
8
# Commit with a message
git commit -m "My message"

# Automatically stage modified and removed files and commit
git commit -a

# Reuse commit info from the log
git commit -c

Align changes

Rebase

1
2
3
4
5
6
7
8
9
10
# Align changes with `main` branch
git checkout feature/my-feature
git rebase main
git push --force-with-lease

# Interactive rebase
git rebase -i main

# Rebase a branch onto a different branch
git rebase –onto new-branch-name old-branch-name

Copy changes

Merge

Please check the section on Merge branches.

Cherry-pick

1
2
3
4
5
# Copy commit
git cherry-pick <hash>

# Copy commit changes without committing
git cherry-pick -n <hash>

Undo changes

Staged changes

1
2
# Unstage a file
git reset staged-file-name.txt

Uncommitted changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Undo changes for a single file
git checkout -- file.name

# Undo changes for all files
git checkout -- .

# Remove untracked files
git clean -df

# Remove untracked and ignored files
git clean -dfX

# Show what would be removed
git clean -n

Committed changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Update last commit
git commit --amend

# Revert to a previous state by removing commits
git commit -m "Something terribly misguided"
git reset HEAD~
# ... edit files as necessary ...
git add .
git commit -c ORIG_HEAD

# Undo reset HEAD~
git reset HEAD@{1}

# Create a commit right away undoing changes
git revert <hash>

# Undo a commit and leave changes uncommitted
git revert -n 3ec5bed

# Abort incomplete reverting
git revert --abort

Postpone changes

Create stash

1
2
3
4
5
6
7
8
9
10
11
# Stash tracked changes only
git stash

# Stash with untracked changes
git stash -u

# Stash with untracked and ignored files
git stash -a

# Stash changes with a comment
git stash -m “My changes”

View stashes

1
2
3
4
5
6
7
8
# View stashes list
git stash list

# View stash summary
git stash show stash@{0}

# View stash details
git stash show -p stash@{0}

Apply stash

1
2
3
4
5
# Apply stash and preserve it in list
git stash apply stash@{0}

# Apply last stash and remove it
git stash pop

Remove stash

1
2
# Remove stash
 git stash drop stash@{0}

Share changes

Share as a patch file

1
2
3
4
5
6
7
8
9
# Create `name.patch` patch file with changes produced by `diff`
git diff > name.patch
git diff HEAD > name.patch

# Include new file into patch
git add -N new-file.txt

# Create patch for last commit
git diff HEAD^ HEAD > name.patch

Apply patch

1
git apply name.patch

Inspect changes

Check repository state

1
git status

Show uncommitted changes

1
2
3
4
5
# Show changes for tracked but not staged changes
git diff

# Show changes for the file
git diff changed.file

Compare changes between commits

1
2
3
4
5
# Show changes from last commit
git diff HEAD^ HEAD

# Compare two commits
git diff <hash1> <hash2>

Compare changes between branches

1
2
# Compare `main` and `dev` branches
git diff main..dev

View commit history

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Show history
git log --graph --abbrev-commit

# Show changes for a particular file
git log file.name

# Show changes for a particular folder
git log folder-name/

# View commit details and diff
git show 3a5sd4

# View per line author information
git blame -w file.name

Branching and merging

Create branch

1
git checkout -b feature/new-feature

Rename branch

1
2
# Rename local branch
git branch -m new-name

Switch branches

1
2
3
4
5
# Switch to a branch
git checkout branch-name

# Switch back
git checkout -

Merge branches

1
2
3
4
5
6
7
8
# Merge branch into current one
git checkout main
git merge feature/my-feature

# Squash merge
git checkout main
git merge --squash feature/my-feature
git commit -m "Add feature X (squashed commit)"

Delete branches

1
2
3
4
5
# Delete local branch if it's merged in the upstream
git branch -d branch-name

# Delete local branch anyways
git branch -D branch-name

Remote repositories

List remotes

1
2
3
4
5
# List all remotes
git remote show

# Show details on `origin` remote
git remote show origin

Add remote

1
2
3
4
5
# Add remote for the first time
git remote add origin https://github.com/kungfux/project.git

# Add remote with a new name
git remote add new-home https://github.com/kungfux/project.git

Push changes to remote

1
git push -u origin branch-name

Fetch and pull from remote

1
2
3
4
5
# Fetch updates from remote
git fetch

# Get updates from remote
git pull

Remove remote branch

1
git push origin --delete branch-name

Remove remote

1
git remote remove origin

Recipes

Update user email after committing the wrong one

1
2
3
git config user.email "kungfux@users.noreply.github.com"
git commit --amend --reset-author --no-edit
git push

Move git repository to new home

1
2
3
4
5
git remote add new-home https://github.com/kungfux/project.git
git push --all new-home
git push --tags new-home
git remote remove origin
git remote rename new-home origin

Skip SSL verification for a single command

1
git -c http.sslVerify=false clone https://github.com/kungfux/project.git

Visual tools

The routine operations and diff are easier to execute from GUI applications. The applications like VS Code, GitHub Desktop and maybe others made a great job on making interaction with Git a much easier. Here are some tools I would recommend:

  • VS Code5
  • VS Code extensions
    • mhutchie.git-graph
    • pomber.git-file-history
  • GitHub Desktop6

References

This post is licensed under CC BY 4.0 by the author.