107 lines
3.6 KiB
Markdown
107 lines
3.6 KiB
Markdown
# Module 05: Merge Conflicts
|
|
|
|
## Learning Objectives
|
|
|
|
In this module, you will:
|
|
- Understand what merge conflicts are and why they occur
|
|
- Recognize conflict markers in files
|
|
- Manually resolve merge conflicts
|
|
- Complete a merge after resolving conflicts
|
|
- Use `git status` to identify conflicted files
|
|
|
|
## Challenge
|
|
|
|
### Setup
|
|
|
|
Run the setup script to create your challenge environment:
|
|
|
|
```powershell
|
|
.\setup.ps1
|
|
```
|
|
|
|
This will create a `challenge/` directory with a Git repository that has conflicting changes on two branches.
|
|
|
|
### Your Task
|
|
|
|
You will intentionally create and resolve a merge conflict - a crucial skill for collaborative development!
|
|
|
|
**The Scenario:**
|
|
- The `main` branch and `feature-updates` branch both modified the same lines in `app.py`
|
|
- When you try to merge them, Git can't automatically resolve the differences
|
|
- You must manually resolve the conflict
|
|
|
|
**Steps:**
|
|
|
|
1. Navigate to the challenge directory: `cd challenge`
|
|
2. Verify you're on main: `git branch`
|
|
3. Attempt to merge feature-updates: `git merge feature-updates`
|
|
4. Git will report a conflict! Don't panic - this is expected
|
|
5. Check status: `git status` (shows conflicted files)
|
|
6. Open `app.py` in a text editor
|
|
7. Look for conflict markers:
|
|
```
|
|
<<<<<<< HEAD
|
|
(your current branch's version)
|
|
=======
|
|
(the other branch's version)
|
|
>>>>>>> feature-updates
|
|
```
|
|
8. Manually resolve by editing the file:
|
|
- Remove the conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`)
|
|
- Keep the version you want, or combine both changes
|
|
9. Save the file
|
|
10. Mark as resolved: `git add app.py`
|
|
11. Complete the merge: `git commit`
|
|
12. View the result: `git log --oneline --graph`
|
|
|
|
> **Important Notes:**
|
|
> - Conflicts occur when the **same lines** are changed differently in both branches
|
|
> - Conflict markers show both versions - you choose what to keep
|
|
> - You can keep one version, the other, or combine them creatively
|
|
> - After editing, you MUST `git add` to mark the conflict as resolved
|
|
> - Then `git commit` to complete the merge
|
|
> - Use `git merge --abort` if you want to cancel and start over
|
|
|
|
## Key Concepts
|
|
|
|
- **Merge Conflict**: Occurs when the same lines in a file are changed differently in both branches being merged.
|
|
- **Conflict Markers**: Special markers Git inserts to show the conflicting versions:
|
|
- `<<<<<<< HEAD`: Marks the start of your current branch's changes
|
|
- `=======`: Separates the two conflicting versions
|
|
- `>>>>>>> branch-name`: Marks the end of the other branch's changes
|
|
- **Conflict Resolution**: The process of manually editing the file to resolve the differences and remove conflict markers.
|
|
- **Staging Resolution**: Using `git add` to tell Git you've resolved the conflict.
|
|
|
|
## Useful Commands
|
|
|
|
```bash
|
|
git merge <branch> # Attempt merge (may cause conflict)
|
|
git status # See which files have conflicts
|
|
git diff # View conflicts in detail
|
|
cat <file> # View file with conflict markers
|
|
git add <file> # Mark conflict as resolved
|
|
git commit # Complete the merge
|
|
git commit -m "message" # Complete merge with message
|
|
git merge --abort # Cancel merge and go back
|
|
```
|
|
|
|
## Verification
|
|
|
|
Once you've resolved the conflict and completed the merge, verify your solution:
|
|
|
|
```powershell
|
|
.\verify.ps1
|
|
```
|
|
|
|
The verification script will check that you've successfully resolved the conflict and completed the merge.
|
|
|
|
## Need to Start Over?
|
|
|
|
If you want to reset the challenge and start fresh:
|
|
|
|
```powershell
|
|
.\reset.ps1
|
|
```
|
|
|
|
This will remove the challenge directory and run the setup script again, giving you a clean slate.
|