Merging Branches and Resolving Conflicts
When your feature is complete, you merge its changes back into your main branch. This process can sometimes result in conflicts.
1. Merging Branches
To merge a feature branch into the main branch:
# 1. Switch to the target branch you want to merge changes into
git switch main
# 2. Merge the feature branch
git merge feature-login- Fast-Forward Merge: Occurs if the target branch has no new commits since the feature branch was created. Git simply moves the branch pointer forward.
- Three-Way Merge: Occurs if both branches have new commits. Git creates a new merge commit combining both histories.
2. Resolving Merge Conflicts
If you and another developer edit the same line of the same file in different commits, Git will pause the merge and flag a conflict:
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.Understanding Conflict Markers
Open the conflicted file in your editor to locate the markers:
<<<<<<< HEAD
<h1>Welcome to retail shop</h1>
=======
<h1>Welcome to WebStore</h1>
>>>>>>> feature-login<<<<<<< HEAD: Marks the start of the changes from your target branch.=======: The boundary line separating your changes from the incoming branch.>>>>>>> feature-login: Marks the end of the changes from the incoming branch.
Resolution Steps
- Edit the file to choose the preferred code layout. Remove the conflict markers entirely.
- Stage the resolved files:
git add index.html. - Commit the merge to finish:
git commit -m "merge: resolve conflicts".
Published on Last updated: