Branch Operations
Essential Git Branch Management Commands
Branch operations are critical for effective version control and collaborative software development. Understanding how to create, switch, merge, and delete branches enables developers to manage complex project workflows efficiently.
Creating and Switching Branches
## Create a new branch
git branch feature-authentication
## Switch to a branch
git checkout feature-authentication
## Create and switch in one command
git checkout -b feature-payment-gateway
Branch Listing and Inspection
## List local branches
git branch
## List all branches (local and remote)
git branch -a
## Show current branch details
git branch -v
Merging Branches
gitGraph
commit
branch feature-login
checkout feature-login
commit
commit
checkout main
merge feature-login
Merge Strategies
Merge Type |
Command |
Description |
Fast-Forward |
git merge feature-branch |
Simple linear merge |
Three-Way Merge |
git merge --no-ff feature-branch |
Creates merge commit |
Rebase |
git rebase main |
Restructures commit history |
Advanced Branch Operations
## Delete a local branch
git branch -d feature-branch
## Force delete an unmerged branch
git branch -D feature-branch
## Rename a branch
git branch -m old-name new-name
Handling Remote Branches
## Push a new branch to remote
git push -u origin feature-branch
## Delete a remote branch
git push origin --delete feature-branch
Conflict Resolution During Merging
When branches have conflicting changes, Git requires manual intervention:
## Merge branches
git merge feature-branch
## If conflicts occur, resolve them manually
## Edit conflicting files
git add resolved-files
git commit