~/blog/git-commands
1 min left0%
Cheatsheetยท1 min read

Git Commands Cheatsheet

Published Jan 5, 2024

Git Commands Cheatsheet

A quick reference for the Git commands I use most often.

Basic Commands

Setup & Configuration

# Set user name and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name
git config --global init.defaultBranch main

# List all configuration
git config --list
# Set user name and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name
git config --global init.defaultBranch main

# List all configuration
git config --list

Creating & Cloning

# Initialize new repository
git init

# Clone existing repository
git clone <url>
git clone <url> <directory>

# Clone specific branch
git clone -b <branch> <url>
# Initialize new repository
git init

# Clone existing repository
git clone <url>
git clone <url> <directory>

# Clone specific branch
git clone -b <branch> <url>

Daily Workflow

Staging & Committing

# Check status
git status

# Stage files
git add <file>
git add .
git add -p  # Interactive staging

# Commit changes
git commit -m "message"
git commit -am "message"  # Add and commit tracked files

# Amend last commit
git commit --amend
git commit --amend --no-edit
# Check status
git status

# Stage files
git add <file>
git add .
git add -p  # Interactive staging

# Commit changes
git commit -m "message"
git commit -am "message"  # Add and commit tracked files

# Amend last commit
git commit --amend
git commit --amend --no-edit

Branching

# List branches
git branch
git branch -a  # Include remote branches

# Create and switch to branch
git checkout -b <branch>
git switch -c <branch>  # Modern syntax

# Switch branches
git checkout <branch>
git switch <branch>

# Delete branch
git branch -d <branch>
git branch -D <branch>  # Force delete
# List branches
git branch
git branch -a  # Include remote branches

# Create and switch to branch
git checkout -b <branch>
git switch -c <branch>  # Modern syntax

# Switch branches
git checkout <branch>
git switch <branch>

# Delete branch
git branch -d <branch>
git branch -D <branch>  # Force delete

Advanced Operations

Rebasing

# Rebase onto branch
git rebase <branch>

# Interactive rebase (last N commits)
git rebase -i HEAD~N

# Continue after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort
# Rebase onto branch
git rebase <branch>

# Interactive rebase (last N commits)
git rebase -i HEAD~N

# Continue after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

Stashing

# Stash changes
git stash
git stash push -m "message"

# List stashes
git stash list

# Apply stash
git stash pop
git stash apply stash@{n}

# Drop stash
git stash drop stash@{n}
# Stash changes
git stash
git stash push -m "message"

# List stashes
git stash list

# Apply stash
git stash pop
git stash apply stash@{n}

# Drop stash
git stash drop stash@{n}

Undoing Changes

# Discard changes in working directory
git checkout -- <file>
git restore <file>

# Unstage files
git reset HEAD <file>
git restore --staged <file>

# Reset to specific commit
git reset --soft HEAD~1   # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1   # Discard changes
# Discard changes in working directory
git checkout -- <file>
git restore <file>

# Unstage files
git reset HEAD <file>
git restore --staged <file>

# Reset to specific commit
git reset --soft HEAD~1   # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1   # Discard changes

Useful Aliases

Add these to your .gitconfig:

[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph --all
    unstage = reset HEAD --
    last = log -1 HEAD
    undo = reset --soft HEAD~1
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph --all
    unstage = reset HEAD --
    last = log -1 HEAD
    undo = reset --soft HEAD~1

Pro Tips

  1. Use git log --oneline --graph for a visual commit history
  2. Use git diff --staged to see what you're about to commit
  3. Use git reflog to recover "lost" commits
  4. Use git bisect to find which commit introduced a bug
  5. Use git blame <file> to see who changed what

Keep this handy for quick reference during your daily Git workflow!