Branching and Merging
Branches let you develop features in isolation and keep the main line of development stable. Merging integrates changes from one branch into another.
Branching
Create a new branch and switch to it:
git checkout -b feature/my-feature
List branches (current branch marked with *):
git branch
Delete a branch locally:
git branch -d feature/my-feature
Merging
To merge a feature branch into main:
git checkout main
git merge feature/my-feature
If Git cannot automatically combine changes you'll get a merge conflict that must be resolved by editing the conflicting files, staging, and committing.
Resolving Conflicts
- Git marks conflicts with <<<<<<<, =======, >>>>>>> markers inside files.
- Edit to choose or combine changes, then run
git add <file>andgit committo finish.
Rebase vs Merge
Rebasing rewrites commits to create a linear history:
git checkout feature/my-feature
git rebase main
Use rebase to keep history linear for private branches; avoid rebasing public/shared branches.