View All Cheat Sheets
Seiwa Holdings Seiwa Holdings

Git Commands Cheat Sheet

A comprehensive reference for common Git commands and workflows

git version-control development

Git Commands Cheat Sheet

Basic Commands

Setup and Configuration

# Initialize a new Git repository
git init

# Clone a repository
git clone <repository-url>

# Configure user information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Basic Workflow

# Check status of working directory
git status

# Add files to staging area
git add <file>
# Add all files
git add .

# Commit changes
git commit -m "Commit message"

# Push changes to remote repository
git push origin <branch-name>

# Pull changes from remote repository
git pull origin <branch-name>

Branch Management

# List all branches
git branch

# Create a new branch
git branch <branch-name>

# Switch to a branch
git checkout <branch-name>

# Create and switch to a new branch
git checkout -b <branch-name>

# Merge a branch into current branch
git merge <branch-name>

# Delete a branch
git branch -d <branch-name>

Advanced Commands

# View commit history
git log

# View compact commit history
git log --oneline

# View commit history with graph
git log --graph --oneline --all

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

# Unstage a file
git reset HEAD <file>

# Amend the most recent commit
git commit --amend

# Interactive rebase
git rebase -i HEAD~<number-of-commits>

Stashing Changes

# Stash current changes
git stash

# Stash with a message
git stash save "stash message"

# List all stashes
git stash list

# Apply most recent stash without removing it
git stash apply

# Apply specific stash without removing it
git stash apply stash@{n}

# Apply and remove most recent stash
git stash pop

# Apply and remove specific stash
git stash pop stash@{n}

# Remove most recent stash
git stash drop

# Remove specific stash
git stash drop stash@{n}

# Clear all stashes
git stash clear

Advanced Branching and Merging

# Create a branch from a specific commit
git branch <branch-name> <commit-hash>

# Cherry-pick a commit to current branch
git cherry-pick <commit-hash>

# Merge with commit message
git merge --no-ff -m "Merge message" <branch-name>

# Abort a merge with conflicts
git merge --abort

# Rebase current branch onto another branch
git rebase <branch-name>

# Continue rebase after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

Git Bisect for Debugging

# Start bisect process
git bisect start

# Mark current commit as bad
git bisect bad

# Mark a known good commit
git bisect good <commit-hash>

# Mark current commit as good during bisect
git bisect good

# Mark current commit as bad during bisect
git bisect bad

# End bisect process
git bisect reset

# Automate bisect with a test script
git bisect run <test-script>

Working with Remote Repositories

# Add a remote repository
git remote add <name> <url>

# List remote repositories
git remote -v

# Fetch changes from a remote repository
git fetch <remote>

# Fetch all remotes
git fetch --all

# Pull with rebase instead of merge
git pull --rebase <remote> <branch>

# Push to a remote branch
git push <remote> <local-branch>:<remote-branch>

# Push and set upstream
git push -u <remote> <branch>

# Delete a remote branch
git push <remote> --delete <branch>

Git Hooks and Automation

# Location of hooks directory
.git/hooks/

# Common hook types
pre-commit       # Run before commit is created
prepare-commit-msg # Modify default commit message
commit-msg      # Validate commit message format
post-commit     # Run after commit is created
pre-push        # Run before push is executed
pre-rebase      # Run before rebase is executed

Git Configuration and Customization

# Set global Git username
git config --global user.name "Your Name"

# Set global Git email
git config --global user.email "your.email@example.com"

# Set Git editor
git config --global core.editor "code --wait"

# Create Git alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

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

# Enable Git credential helper
git config --global credential.helper cache

# Set credential cache timeout (in seconds)
git config --global credential.helper 'cache --timeout=3600'