Git Cheat Sheet

Essential Git commands for version control - branching, committing, merging, and collaboration.

Setup & Configuration

Configure Git and set up your identity

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

# Check your configuration
git config --list

# Set default branch name
git config --global init.defaultBranch main
Initial setup
# Set default editor
git config --global core.editor "code --wait"

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

# Set up credential helper (Windows)
git config --global credential.helper wincred
Additional configuration

Creating Repositories

Initialize or clone Git repositories

# Initialize a new repository
git init

# Initialize with specific branch name
git init -b main

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

# Clone into a specific directory
git clone https://github.com/user/repo.git my-folder
Repository initialization

Basic Workflow

Common commands for daily Git usage

# Check repository status
git status

# Add files to staging area
git add file.txt
git add .                 # Add all files
git add *.js              # Add all JavaScript files

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

# View commit history
git log
git log --oneline         # Compact view
git log --graph           # Show branch graph
Daily commands

Branching & Merging

Create and manage branches

# List branches
git branch                # Local branches
git branch -a             # All branches (local + remote)

# Create a new branch
git branch feature-branch

# Switch to a branch
git checkout feature-branch
git switch feature-branch  # Modern alternative

# Create and switch to new branch
git checkout -b feature-branch
git switch -c feature-branch
Branch management
# Merge a branch into current branch
git merge feature-branch

# Delete a branch
git branch -d feature-branch    # Safe delete
git branch -D feature-branch    # Force delete

# Rename a branch
git branch -m old-name new-name
Merging and cleanup

Remote Repositories

Work with remote repositories

# List remote repositories
git remote -v

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

# Fetch changes from remote
git fetch origin

# Pull changes from remote
git pull origin main

# Push changes to remote
git push origin main
git push -u origin main   # Set upstream and push
Remote operations
# Remove a remote
git remote remove origin

# Rename a remote
git remote rename old-name new-name

# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git
Remote management

Undoing Changes

Revert or reset changes

# Discard changes in working directory
git restore file.txt
git checkout -- file.txt  # Old way

# Unstage files
git restore --staged file.txt
git reset HEAD file.txt   # Old way

# Amend last commit
git commit --amend -m "New message"

# Revert a commit (creates new commit)
git revert <commit-hash>
Undo operations
# Reset to previous commit (careful!)
git reset --soft HEAD~1   # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1   # Discard all changes

# Reset to specific commit
git reset --hard <commit-hash>
Reset operations

Stashing

Temporarily save changes

# Stash current changes
git stash
git stash save "Work in progress"

# List stashes
git stash list

# Apply stash
git stash apply           # Keep stash
git stash pop             # Apply and remove stash

# Apply specific stash
git stash apply stash@{0}

# Delete stash
git stash drop stash@{0}
git stash clear           # Delete all stashes
Stash management

Viewing Changes

Inspect differences and history

# Show unstaged changes
git diff

# Show staged changes
git diff --staged
git diff --cached

# Show changes between commits
git diff commit1 commit2

# Show changes in a specific file
git diff file.txt
Diff commands
# Show commit details
git show <commit-hash>

# Show file at specific commit
git show <commit-hash>:file.txt

# Show who changed what in a file
git blame file.txt
Inspection commands

Tagging

Create and manage version tags

# List tags
git tag

# Create lightweight tag
git tag v1.0.0

# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"

# Tag a specific commit
git tag -a v1.0.0 <commit-hash> -m "Message"

# Push tags to remote
git push origin v1.0.0
git push origin --tags    # Push all tags

# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0
Tag operations

Useful Commands

Additional helpful Git commands

# Clean untracked files
git clean -n              # Dry run
git clean -f              # Remove files
git clean -fd             # Remove files and directories

# Search in repository
git grep "search term"

# Show who made changes to a file
git log --follow file.txt

# Create alias
git config --global alias.st status
git config --global alias.co checkout
Helpful utilities
# Ignore file changes
git update-index --assume-unchanged file.txt

# Track file changes again
git update-index --no-assume-unchanged file.txt

# Show tracked files
git ls-files
File tracking
Git Cheat Sheet