Git Cheat Sheet

Git is the free and open-source distributed version control system at the heart of GitHub and most modern development workflows. This cheat sheet covers the most important and commonly used Git commands for easy reference.

Setup

Configure user information used across all local repositories.

# Set your name for commit history
git config --global user.name "Firstname Lastname"

# Set your email for commit history
git config --global user.email "you@example.com"

# Enable colorized output
git config --global color.ui auto

# Cache credentials (stored in plaintext — use a credential manager in production)
git config --global credential.helper store

Initialize a Repository

# Initialize a new local repo
git init

# Clone an existing repo
git clone https://github.com/user/repo.git

Create a New Repo on GitHub via CLI

echo "# my-project" >> README.md
git init
git add .
git commit -m "init commit"
git branch -M main

# Create the remote repo via GitHub API
curl -u USER:TOKEN https://api.github.com/user/repos \
  -d '{"name":"PROJECTNAME","description":"Project description","private":true}'

git remote add origin https://github.com/USER/PROJECTNAME.git
git push -u origin main

Staging & Committing

# Show modified files in working directory
git status

# Stage a specific file
git add [file]

# Stage all changes
git add .

# Unstage a file (keep changes in working directory)
git reset [file]

# Show unstaged changes
git diff

# Show staged (not yet committed) changes
git diff --staged

# Commit staged changes
git commit -m "your message"

Branching & Merging

# List all branches (* = current branch)
git branch

# Create a new branch
git branch [branch-name]

# Switch to a branch
git checkout [branch-name]

# Create and switch in one step
git checkout -b [branch-name]

# Merge a branch into the current branch
git merge [branch]

# Delete a branch
git branch -d [branch-name]

Remote Operations

# Add a remote
git remote add origin https://github.com/user/repo.git

# List remotes
git remote -v

# Fetch all branches from remote (no merge)
git fetch origin

# Fetch and merge from tracking branch
git pull

# Push local branch to remote
git push origin [branch]

# Merge a remote branch into current branch
git merge origin/[branch]

Inspect & Compare

# Show commit history for current branch
git log

# Show commits on branchA not on branchB
git log branchB..branchA

# Show commits that changed a file (even across renames)
git log --follow [file]

# Show diff between two branches
git diff branchB...branchA

# Show any Git object in human-readable format
git show [SHA]

Tracking Path Changes

# Delete a file and stage the removal
git rm [file]

# Rename/move a file and stage the change
git mv [old-path] [new-path]

# Show commit log with path changes indicated
git log --stat -M

Rewriting History

# Rebase current branch ahead of another
git rebase [branch]

# Reset staging area and working tree to a specific commit (destructive)
git reset --hard [commit]

Temporary Commits (Stash)

# Save modified and staged changes to stash
git stash

# List all stash entries
git stash list

# Apply the top stash entry and remove it from the stack
git stash pop

# Discard the top stash entry without applying
git stash drop

Ignoring Files

Create a .gitignore file in your repo root with patterns to exclude:

logs/
*.notes
temp-*/
# Set a global gitignore for all local repos
git config --global core.excludesfile ~/.gitignore_global