# Set user info
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait"
# View configuration
git config --list
git config user.name
# Initialize new repository
git init
# Clone existing repository
git clone <url>
git clone <url> <directory>
# Clone specific branch
git clone -b <branch> <url>
# Shallow clone (faster)
git clone --depth 1 <url>
# Check status
git status
git status -s # Short format
# Add files to staging
git add <file>
git add . # Add all files
git add -p # Interactive staging
# Commit changes
git commit -m "message"
git commit -am "message" # Add + commit (tracked files)
git commit --amend # Modify last commit
# View commit history
git log
git log --oneline
git log --graph --oneline --all
git log -p # Show patches
git log -n 5 # Last 5 commits
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create branch
git branch <name>
# Switch branch
git checkout <branch>
git switch <branch> # Git 2.23+
# Create and switch
git checkout -b <name>
git switch -c <name> # Git 2.23+
# Rename branch
git branch -m <old> <new>
# Delete branch
git branch -d <name> # Safe delete
git branch -D <name> # Force delete
# Delete remote branch
git push origin --delete <branch>
# Merge branch into current
git merge <branch>
# Merge with no fast-forward
git merge --no-ff <branch>
# Abort merge
git merge --abort
# Rebase current branch
git rebase <branch>
# Interactive rebase
git rebase -i HEAD~3
# Abort rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
# List remotes
git remote -v
# Add remote
git remote add <name> <url>
# Remove remote
git remote remove <name>
# Rename remote
git remote rename <old> <new>
# Fetch changes
git fetch
git fetch <remote>
git fetch --all
# Pull changes
git pull
git pull --rebase
# Push changes
git push
git push -u origin <branch> # Set upstream
git push --force # Force push (careful!)
git push --force-with-lease # Safer force push
# Stash changes
git stash
git stash -m "message"
git stash -u # Include untracked files
# List stashes
git stash list
# Apply stash
git stash apply # Keep in stash list
git stash pop # Remove from stash list
git stash apply stash@{1}
# Drop stash
git stash drop stash@{0}
git stash clear # Drop all
# Unstage files
git reset HEAD <file>
git restore --staged <file>
# Discard changes in working directory
git checkout -- <file>
git restore <file>
# Reset to previous commit
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged
git reset --hard HEAD~1 # Discard changes
# Revert commit (creates new commit)
git revert <commit>
# Restore deleted file
git checkout <commit> -- <file>
# Working directory vs staging
git diff
# Staging vs last commit
git diff --staged
git diff --cached
# Compare commits
git diff <commit1> <commit2>
# Compare branches
git diff <branch1>..<branch2>
# Show specific commit
git show <commit>
git show HEAD~2
# List tags
git tag
# Create lightweight tag
git tag <name>
# Create annotated tag
git tag -a <name> -m "message"
# Tag specific commit
git tag <name> <commit>
# Push tags
git push origin <tag>
git push --tags
# Delete tag
git tag -d <name>
git push origin --delete <tag>
# Apply specific commit to current branch
git cherry-pick <commit>
# Cherry-pick without committing
git cherry-pick --no-commit <commit>
# Cherry-pick range
git cherry-pick <start>..<end>
# Show who changed what
git blame <file>
# Search commits
git log --grep="pattern"
git log -S "code" # Search for code changes
# Find commit that introduced bug
git bisect start
git bisect bad
git bisect good <commit>
# Clean untracked files
git clean -n # Dry run
git clean -f # Force clean
git clean -fd # Include directories
# Create archive
git archive --format=zip HEAD > archive.zip