Git Cheatsheet

Essential Git commands for version control

← Back to Cheatsheets

Setup and Configuration

Global Configuration
# Set global username git config --global user.name "Your Name" # Set global email git config --global user.email "you@example.com" # Set default editor git config --global core.editor "code --wait" # List all global settings git config --global --list # Set default branch name git config --global init.defaultBranch main
Global Git configuration
Repository Setup
# Initialize a new repository git init # Clone an existing repository git clone https://github.com/user/repo.git # Clone to a specific directory git clone https://github.com/user/repo.git my-project # Clone a specific branch git clone -b branch-name https://github.com/user/repo.git
Setting up Git repositories

Basic Snapshotting

Working with Files
# Check repository status git status # Add files to staging git add file.txt git add *.js git add . # Add all changes # Remove files git rm file.txt git rm --cached file.txt # Remove from tracking only # Move/rename files git mv old.txt new.txt # Commit changes git commit -m "Commit message" git commit -a -m "Commit all tracked files"
Basic file operations
Viewing Changes
# View changes in working directory git diff # View staged changes git diff --staged git diff --cached # View changes for specific file git diff file.txt # Compare branches git diff branch1..branch2 # View commit history git log git log --oneline git log -5 # Last 5 commits git log --author="John" git log --since="2 weeks ago"
Viewing changes and history

Branching and Merging

Branch Management
# List branches git branch git branch -r # Remote branches git branch -a # All branches # Create a new branch git branch feature-branch # Switch to a branch git checkout feature-branch git switch feature-branch # Newer syntax # Create and switch in one command git checkout -b feature-branch git switch -c feature-branch # Newer syntax # Delete a branch git branch -d feature-branch # Safe delete git branch -D feature-branch # Force delete
Branch creation and management
Merging
# Merge a branch git checkout main git merge feature-branch # Merge without fast-forward git merge --no-ff feature-branch # Abort a merge (if conflicts) git merge --abort # Rebase (alternative to merge) git checkout feature-branch git rebase main # Abort a rebase git rebase --abort # Continue after resolving conflicts git rebase --continue
Branch merging strategies
Remote Branches
# List remote repositories git remote -v # Add a remote git remote add origin https://github.com/user/repo.git # Fetch changes from remote git fetch origin git fetch origin branch-name # Pull changes (fetch + merge) git pull origin main git pull --rebase origin main # Push changes git push origin main git push -u origin feature-branch # Set upstream git push origin --delete branch-name # Delete remote branch
Working with remote repositories

Undoing Changes

Undoing Working Directory Changes
# Discard changes in working directory git checkout -- file.txt git restore file.txt # Newer syntax # Discard all changes git checkout -- . git restore . # Newer syntax # Unstage files git reset HEAD file.txt git restore --staged file.txt # Newer syntax # Undo last commit (keep changes) git reset --soft HEAD~1 # Undo last commit and changes git reset --hard HEAD~1 # Revert a specific commit git revert commit-hash
Reverting and undoing changes
Advanced Undoing
# Reset to specific commit git reset --hard commit-hash # Reset to remote state git fetch origin git reset --hard origin/main # Recover deleted commits git reflog git reset --hard HEAD@{1} # Cherry-pick specific commits git cherry-pick commit-hash # Create a tag git tag v1.0 git tag -a v1.0 -m "Version 1.0" git push origin v1.0
Advanced recovery techniques

Stashing and Cleaning

Stashing Changes
# Stash current changes git stash # Stash with description git stash save "Work in progress" # List stashes git stash list # Apply latest stash git stash apply # Apply specific stash git stash apply stash@{1} # Apply and remove stash git stash pop # Drop a stash git stash drop git stash drop stash@{1} # Clear all stashes git stash clear
Temporarily saving changes
Cleaning
# Remove untracked files git clean -f # Remove untracked files and directories git clean -fd # Dry run (see what would be removed) git clean -n # Remove ignored files too git clean -fdx # Interactive cleaning git clean -fdi
Removing untracked files

Inspection and Comparison

Viewing History
# Detailed log git log --stat git log -p # Show patches git log --graph --oneline --all # Search commit messages git log --grep="bugfix" # Search in commit content git log -S "function_name" # Show specific commit git show commit-hash git show HEAD~2 # View file history git log --follow file.txt # Compare commits git diff commit1 commit2
Detailed history inspection
Blame and Bisect
# See who changed each line git blame file.txt # See who changed specific lines git blame -L 10,20 file.txt # Binary search for bug git bisect start git bisect bad # Current commit is bad git bisect good v1.0 # Last good commit # Test the code, then: git bisect bad # If still bad git bisect good # If good git bisect reset # Exit bisect mode
Finding when bugs were introduced