Git Merge — Overview
### 🎯 Learning Objectives
After completing this module, you should be able to:
* Commit changes in Git
* Merge changes between branches
* Push to a Git repository
* Pull from a Git repository
---
## 📘 Description
### 🔹 What is Git Merge?
* Merging is Git’s way of combining changes from different branches into one.
* The `git merge` command integrates the changes from one branch (like `feature`)
into another branch (like `main`).
* Merging helps teams combine work done in parallel into a single unified codebase.
---
### ⚙️ How It Works
1. Git identifies the common base commit between the two branches.
2. It then creates a new merge commit that combines changes from both.
3. If both branches changed the same part of a file, merge conflicts occur and must
be resolved manually.
🧠 Example:
```bash
# Switch to main branch (receiving branch)
git checkout main
# Merge changes from feature branch into main
git merge feature
```
* If everything merges smoothly → a merge commit is created automatically.
* If not → Git will mark conflicted sections for you to fix.
---
## 🧱 Preparing to Merge
1. Confirm the receiving branch
```bash
git status
git checkout main
```
2. Fetch the latest remote commits
```bash
git fetch
git pull
```
3. Initiate merge
```bash
git merge <branch-name>
```
---
## ⚠️ Merge Conflicts
When two branches edit the same part of a file, Git cannot decide which version to
keep.
Git marks these conflicts like this:
```text
<<<<<<< HEAD
print("Hello from main branch")
=======
print("Hello from feature branch")
>>>>>>> feature
```
You must manually edit and remove these conflict markers, keeping only the correct
version.
---
## 🧰 Resolving Merge Conflicts
### 🔸 On GitHub:
1. Go to Pull Requests → Choose PR → Resolve Conflicts.
2. Edit the file(s) to remove `<<<<<<<`, `=======`, and `>>>>>>>`.
3. Click Commit merge to complete the merge.
### 🔸 On Git Bash:
1. Navigate to the repository
```bash
cd <repository-name>
```
2. Check for conflicts
```bash
git status
```
3. Open the conflicted file in VS Code or any editor.
4. Fix and save the file (remove the conflict markers).
5. Stage and commit:
```bash
git add .
git commit -m "Resolved merge conflict"
```
---
## ✅ Summary
| Concept | Description
|
| -------------- |
------------------------------------------------------------------ |
| Purpose | Combines multiple branches into one. |
| Command | `git merge <branch-name>` |
| Result | A new merge commit is created. |
| Conflict | Happens when the same part of a file is modified in both branches. |
| Resolution | Manually edit the file and commit the resolved version. |