git: Cheat Sheet

A git cheat sheet containing a small number of commands that may help you on your journey with git.
Hint: Use the filter buttons below to show only the topics you want to focus on.

General
Branching
Committing
Merging
Tagging
Patching

General

# Show global configuration
git config --list

# Set name & email
git config --global user.name "your_username"
git config --global user.email "your_email"

# List remotes
git remote -v

# Add/remove remote
git remote add <remote_name> <URL>
git remote rm <remote_name>

Branching

# Create and checkout branch
git checkout -b <name_of_your_new_branch>

# Create and checkout branch from remote
git checkout -b <some_branch> <some_remote/some_branch>

# List all branches (from remotes also):
git branch -a

# Delete local branch
git branch -d <mybranch>

# Push to remote
git push <remote_name> <branch_name>

# Delete remote ("origin") branch
git push origin --delete <mybranch>

# Delete branches that have been merged. Example: delete merged bugfix branches:
git branch --merged | grep bugfix | xargs git branch -d

Committing

# Show commits overview
git log --graph --oneline [banch_name]

git log --graph --oneline --first-parent banch_name

# Empty commit. Example:
git commit --allow-empty -m "Empty commit to increase build number"

# Amend last commit
git commit --amend
git push --force-with-lease <repository> <branch>

# Display reference history:
git reflog

Merging

# Abort merge
git merge --abort

Tagging

# List tags
git tag

# Remove all local tags
git tag | xargs git tag -d

# Push tag to remote origin
git push origin <tag>

# Fetch tags
git fetch --tags

# Find all tags contained since certain commit hash
git tag --contains <hash>

Patching

# Example: Create three topmost commits from the current branch.
# The patch files will be saved in current directory.
git format-patch -3

# Extract all commits that lead to origin since the inception of the project
git format-patch --root origin

# Apply a patch
git am <patch_file>