Merging and Resolving Conflicts
💻 GitMerge branches, understand fast-forward versus three-way merges and fix conflicts calmly.
A conflict happens when two branches change the same lines. Git stops and asks you to decide, which is safer than guessing.
- Merge a branch.
- Read conflict markers.
- Resolve and complete a merge.
Merging
Switch to the branch that should receive the work, then merge the other branch into it.
git switch main
git merge feature/about-pageFast-Forward vs Three-Way
If main has no new commits, Git simply moves the pointer forward. If both branches moved, Git creates a merge commit with two parents.
Resolving a Conflict
Git marks the conflicting section. Edit the file to the version you want, remove the markers, then stage and commit.
# after editing the file
git add index.html
git commit -m "Merge feature/about-page and resolve heading conflict"Output
<<<<<<< HEAD
<h1>Welcome to our site</h1>
=======
<h1>Welcome to Courselord</h1>
>>>>>>> feature/about-pageAbort if Needed
If a merge gets confusing, return to the state before it started.
git merge --abortPractice
- Create a conflict on purpose in a test repository and resolve it.
- Explain the difference between fast-forward and three-way merges.
- Abort a merge and confirm the working directory is clean.
Hands-on exercise
1. On main, change a line in README.md and commit.
2. Create a branch, change the same line differently and commit.
3. Merge the branch into main.
4. Resolve the conflict so both ideas are represented, then commit.
Recap
Merging combines lines of work. Conflicts are normal; read both sides, edit deliberately and commit the resolution.
Example
git switch main
git merge feature/about-page
Exercises
2. Create a branch, change the same line differently and commit.
3. Merge the branch into main.
4. Resolve the conflict so both ideas are represented, then commit.
Expected Output
A merge commit with a README that reflects a deliberate decision.
Lesson Nav
Git Course
0/9 lessons completed
- 1 Why Version Control Matters Tutorial
- 2 Your First Repository Tutorial
- 3 Staging and Committing Tutorial
- 4 History, Diffs and Logs Tutorial
- 5 Branching Tutorial
- 6 Merging and Resolving Conflicts Tutorial
- 7 Remotes and Collaboration Tutorial
- 8 Undoing Mistakes Safely Tutorial
- 9 Project: A Team Workflow Project