You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Rebase last 5 commits interactively
git rebase -i HEAD~5
# Rebase onto another branch
git rebase main
# Continue rebase after resolving conflicts
git rebase --continue
# Skip current commit during rebase
git rebase --skip
# Abort rebase and return to original state
git rebase --abort
# Rebase and preserve merge commits
git rebase -p main
# Rebase with autosquash (automatically squash fixup commits)
git rebase -i --autosquash main
# Rebase onto specific commit
git rebase --onto <new-base><old-base><branch>
Rebase vs Merge
# Standard merge (creates merge commit)
git merge feature-branch
# Rebase feature onto main (linear history)
git checkout feature-branch
git rebase main
# Pull with rebase instead of merge
git pull --rebase origin main
Branch Management
Creating and Switching Branches
# Create new branch
git branch feature-name
# Create and switch to new branch
git checkout -b feature-name
# Create and switch (modern syntax)
git switch -c feature-name
# Create branch from specific commit
git branch feature-name <commit-hash># Create orphan branch (no history)
git checkout --orphan new-branch
Switching Branches
# Switch to existing branch
git checkout branch-name
# Switch (modern syntax)
git switch branch-name
# Switch to previous branch
git checkout -
# Force switch (discard local changes)
git checkout -f branch-name
Branch Information
# List all branches
git branch -a
# List remote branches
git branch -r
# List branches with last commit
git branch -v
# List branches with more details
git branch -vv
# Show branches merged into current branch
git branch --merged
# Show branches not merged into current branch
git branch --no-merged
# Show branches that contain specific commit
git branch --contains <commit-hash>
Deleting Branches
# Delete merged branch
git branch -d branch-name
# Force delete branch (even if not merged)
git branch -D branch-name
# Delete remote branch
git push origin --delete branch-name
# Prune deleted remote branches from local
git fetch --prune
git remote prune origin
Merge & Conflict Resolution
Merge Strategies
# Standard merge
git merge feature-branch
# Merge without fast-forward (always create merge commit)
git merge --no-ff feature-branch
# Fast-forward only merge (fail if not possible)
git merge --ff-only feature-branch
# Squash merge (combine all commits into one)
git merge --squash feature-branch
# Use specific merge strategy
git merge -s recursive feature-branch
git merge -s ours feature-branch
git merge -s theirs feature-branch
Conflict Resolution
# Show files with conflicts
git status
# Show conflicts in diff format
git diff
# Show conflicts with more context
git diff --conflict=diff3
# Accept incoming changes for a file
git checkout --theirs <file># Accept current branch changes for a file
git checkout --ours <file># Use merge tool
git mergetool
# Mark conflict as resolved
git add <resolved-file># Continue merge after resolving
git merge --continue
# Abort merge
git merge --abort
Advanced Merge Options
# Merge with custom commit message
git merge -m "Custom message" feature-branch
# Merge and edit commit message
git merge --edit feature-branch
# Merge without committing
git merge --no-commit feature-branch
# Show what would be merged without merging
git merge --no-commit --no-ff feature-branch
git diff --cached
git merge --abort
HEAD Management
Understanding HEAD
# Show what HEAD points to
git symbolic-ref HEAD
# Show HEAD commit
git show HEAD
# Show previous HEAD positions
git reflog show HEAD
Detached HEAD
# Checkout specific commit (creates detached HEAD)
git checkout <commit-hash># Checkout tag (creates detached HEAD)
git checkout v1.0.0
# Create branch from detached HEAD
git checkout -b new-branch-from-detached
# Return to branch (attach HEAD)
git checkout main
Moving HEAD
# Move HEAD to previous commit
git reset HEAD~1
# Move HEAD without changing working directory
git reset --soft HEAD~1
# Move HEAD and update working directory
git reset --hard HEAD~1
# Move HEAD to specific commit
git reset --hard <commit-hash>
Relative References
# Parent of HEAD
git show HEAD^
git show HEAD~1
# Grandparent of HEAD
git show HEAD^^
git show HEAD~2
# Second parent of merge commit
git show HEAD^2
# Great-grandparent
git show HEAD~3
Logging & History
Basic Logs
# Show commit history
git log
# Show last n commits
git log -n 5
# One line per commit
git log --oneline
# Show with graph
git log --graph
# Show all branches
git log --all
# Pretty format
git log --pretty=format:"%h - %an, %ar : %s"
Advanced Log Formatting
# Detailed graph with decorations
git log --graph --oneline --decorate --all
# Show commits with file changes
git log --stat
# Show commits with patches
git log -p
# Show commits affecting specific file
git log -- path/to/file
# Show commits by author
git log --author="John Doe"# Show commits in date range
git log --since="2 weeks ago"
git log --after="2024-01-01" --before="2024-12-31"# Show commits with specific message
git log --grep="fix bug"# Show commits that added or removed specific string
git log -S "function_name"# Show commits that changed specific function
git log -L :function_name:file.js
Visualization Options
# Beautiful graph format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# Show reflog (all reference updates)
git log -g
# Show only merge commits
git log --merges
# Show only non-merge commits
git log --no-merges
# Show commits with word diff
git log -p --word-diff
Searching History
# Find commits that introduced a string
git log -S "search_string" --source --all
# Find commits that changed number of occurrences
git log -G "regex_pattern"# Show files changed in each commit
git log --name-only
# Show files with status
git log --name-status
# Show diff for specific commit
git show <commit-hash># Show specific file from specific commit
git show <commit-hash>:path/to/file
Stashing
Basic Stash Operations
# Stash current changes
git stash
# Stash with message
git stash save "work in progress on feature X"# Stash including untracked files
git stash -u
# Stash including ignored files
git stash -a
# List all stashes
git stash list
# Show stash contents
git stash show
git stash show -p
# Apply most recent stash
git stash apply
# Apply and remove most recent stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
Advanced Stash Operations
# Create branch from stash
git stash branch new-branch-name
# Stash only staged changes
git stash --keep-index
# Stash with patch mode (interactive)
git stash -p
# Drop specific stash
git stash drop stash@{1}
# Clear all stashes
git stash clear
# Show diff of stash
git diff stash@{0}
Reset & Revert
Reset Types
# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (keep changes unstaged) - default
git reset HEAD~1
git reset --mixed HEAD~1
# Hard reset (discard all changes)
git reset --hard HEAD~1
# Reset specific file
git reset HEAD <file># Reset to specific commit
git reset --hard <commit-hash>
Revert Operations
# Revert specific commit (creates new commit)
git revert <commit-hash># Revert without committing
git revert -n <commit-hash># Revert merge commit
git revert -m 1 <merge-commit-hash># Revert range of commits
git revert <oldest-commit>..<newest-commit># Continue revert after resolving conflicts
git revert --continue
# Abort revert
git revert --abort
Clean Operations
# Show what would be removed
git clean -n
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Remove ignored files as well
git clean -fdx
# Interactive clean
git clean -i
Cherry-Pick
Basic Cherry-Pick
# Cherry-pick single commit
git cherry-pick <commit-hash># Cherry-pick multiple commits
git cherry-pick <commit1><commit2># Cherry-pick range of commits
git cherry-pick <start-commit>..<end-commit># Cherry-pick without committing
git cherry-pick -n <commit-hash># Cherry-pick and edit commit message
git cherry-pick -e <commit-hash>
# Show reflog for HEAD
git reflog
# Show reflog with dates
git reflog --date=iso
# Show reflog for specific branch
git reflog show branch-name
# Show all reflogs
git reflog show --all
Recovering Lost Commits
# Find lost commit
git reflog
# Checkout lost commit
git checkout <commit-hash># Create branch from lost commit
git checkout -b recovered-branch <commit-hash># Reset to previous state
git reset --hard HEAD@{2}
Bisect
Binary Search for Bugs
# Start bisect
git bisect start
# Mark current commit as bad
git bisect bad
# Mark known good commit
git bisect good <commit-hash># Mark current commit as good
git bisect good
# Skip current commit
git bisect skip
# Automate bisect with script
git bisect run ./test-script.sh
# End bisect
git bisect reset
# Visualize bisect
git bisect visualize
# Create new worktree
git worktree add ../new-worktree branch-name
# Create worktree with new branch
git worktree add -b new-branch ../new-worktree
# List worktrees
git worktree list
# Remove worktree
git worktree remove ../new-worktree
# Prune worktree information
git worktree prune
# Move worktree
git worktree move <worktree><new-path>
Tags
Creating Tags
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"# Tag specific commit
git tag -a v1.0.0 <commit-hash> -m "Version 1.0.0"# Create signed tag
git tag -s v1.0.0 -m "Signed version 1.0.0"
Managing Tags
# List all tags
git tag
# List tags matching pattern
git tag -l "v1.*"# Show tag information
git show v1.0.0
# Push tag to remote
git push origin v1.0.0
# Push all tags
git push origin --tags
# Delete local tag
git tag -d v1.0.0
# Delete remote tag
git push origin --delete v1.0.0
# Checkout tag
git checkout v1.0.0