Rebase vs Merge
bash
# Merge (preserves history)
git checkout main
git merge feature-branch
# Rebase (linear history)
git checkout feature-branch
git rebase mainWhen to use each:
- Merge: Public branches, preserve history
- Rebase: Local branches, clean history
Interactive Rebase
bash
# Edit last 5 commits
git rebase -i HEAD~5
# Commands:
# pick: Keep commit as-is
# reword: Edit commit message
# edit: Modify commit
# squash: Merge with previous commit
# drop: Remove commitCherry-Pick
bash
# Pick specific commit
git cherry-pick <commit-hash>
# Pick multiple commits
git cherry-pick <commit1> <commit2>
# Pick without committing
git cherry-pick -n <commit-hash>Bisect (Debug)
bash
# Start bisect
git bisect start
git bisect bad # Current version is bad
git bisect good <tag> # Known good version
# Git will checkout commits, test each
git bisect good # Mark as good
git bisect bad # Mark as bad
# When done, find culprit
git bisect resetStash
bash
# Stash changes
git stash
# Stash with message
git stash save "Work in progress"
# Stash untracked files
git stash -u
# List stashes
git stash list
# Apply stash
git stash apply
git stash pop # Apply and remove
# Apply specific stash
git stash apply stash@{1}Reset
bash
# Soft (keep changes)
git reset --soft HEAD~1
# Mixed (default, keep working changes)
git reset --mixed HEAD~1
git reset HEAD~1
# Hard (discard all changes)
git reset --hard HEAD~1
# Reset to remote
git reset --hard origin/mainReflog
bash
# Show reflog
git reflog
# Recover lost commit
git reflog
# Find commit hash
git reset --hard <commit-hash>
# Show branch reflog
git reflog show mainWorktrees
bash
# Create worktree
git worktree add ../feature-branch feature-branch
# List worktrees
git worktree list
# Remove worktree
git worktree remove ../feature-branchSubmodules
bash
# Add submodule
git submodule add https://github.com/user/repo.git lib/repo
# Clone with submodules
git clone --recursive https://github.com/user/main.git
# Update submodules
git submodule update --remote --mergeAliases
bash
# Create 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
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Use aliases
git co main
git lgClean
bash
# Dry-run
git clean -n
# Remove untracked files
git clean -f
# Remove untracked dirs
git clean -fd
# Remove ignored files
git clean -fXMaster Git, master version control!