Back to Home
Git
Branch management, checkout shortcuts and merging strategies
branchesmergerebasecheckoutswitchworkflow
Git Branching
Commands for branch management and merging.
Branch Operations
# List branches
git branch
git branch -a # Include remote branches
git branch -r # Only remote branches
git branch -v # Show last commit on each branch
# Create a branch (does not switch)
git branch feature-name
# Delete branch
git branch -d feature-name
git branch -D feature-name # Force delete
# Rename current branch
git branch -m new-name
# Rename any branch
git branch -m old-name new-name
Checkout and Switch
# Switch to existing branch
git checkout main
git switch main # Modern alternative
# Create and switch in one step
git checkout -b feature/login
git switch -c feature/login
# Create branch from specific commit/tag
git checkout -b hotfix v1.2.0
git checkout -b bugfix abc1234
# Checkout remote branch (auto-track)
git checkout feature/api
git checkout -b local-name origin/remote-name
# Go back to previous branch
git checkout -
git switch -
# Discard changes in a file
git checkout -- file.txt
# Checkout specific file from another branch
git checkout main -- src/config.ts
# Detached HEAD (inspect a commit)
git checkout abc1234
Quick Branch Shortcuts
# Create feature branch from main
git checkout main && git pull && git checkout -b feature/new-feature
# Create hotfix branch from tag
git checkout -b hotfix/fix-bug v2.0.0
# Create branch and push immediately
git checkout -b feature/api && git push -u origin feature/api
# List branches containing a commit
git branch --contains abc1234
# List merged/unmerged branches
git branch --merged main
git branch --no-merged main
# Clean up merged branches
git branch --merged main | grep -v main | xargs git branch -d
Merging
# Merge branch into current
git merge feature-name
# Merge with no fast-forward
git merge --no-ff feature-name
# Abort merge
git merge --abort
Rebasing
# Rebase current branch onto main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
Remote Branches
# Fetch remote branches
git fetch origin
# Track remote branch
git checkout -b local-branch origin/remote-branch
# Push new branch to remote
git push -u origin feature-name
# Delete remote branch
git push origin --delete feature-name