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