|
1 | 1 | ---
|
2 | 2 | Title: 'Merge'
|
3 |
| -Description: 'Merging allows a user to apply all changes from one branch to another when the work is completed.' |
| 3 | +Description: 'Allows a user to apply all changes from one branch to another without losing commit history.' |
4 | 4 | Subjects:
|
5 | 5 | - 'Bash/Shell'
|
6 | 6 | - 'Developer Tools'
|
7 | 7 | Tags:
|
| 8 | + - 'Bash/Shell' |
8 | 9 | - 'Git'
|
9 | 10 | - 'GitHub'
|
| 11 | + - 'Version Control' |
10 | 12 | CatalogContent:
|
11 | 13 | - 'learn-git'
|
12 | 14 | - 'learn-the-command-line'
|
13 | 15 | ---
|
14 | 16 |
|
15 |
| -In Git, completed changes made to a [branch](https://www.codecademy.com/resources/docs/git/branch) can be merged to the `main` branch. When collaborating on a remote repository, a [pull request](https://www.codecademy.com/resources/docs/git/pull-requests) will need to be opened in order to do this. Anyone with push access to the repository can complete the merge. |
| 17 | +The **`git merge`** command allows a user to apply all changes from one [branch](https://www.codecademy.com/resources/docs/git/branch) to another without losing [commit](https://www.codecademy.com/resources/docs/git/commit) history. Whether it is [merging](https://www.codecademy.com/resources/docs/git/merge) a feature branch into the main branch or integrating updates from a teammate, `git merge` ensures that project changes are combined in an organized manner. |
| 18 | + |
| 19 | +## Git Merge Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +git merge <branch-name> |
| 23 | +``` |
| 24 | + |
| 25 | +Here, `<branch-name>` is the name of the branch whose changes are to be merged into the current branch. |
| 26 | + |
| 27 | +**Common optional flags:** |
| 28 | + |
| 29 | +- `--no-ff`: Creates a merge commit even if a fast-forward merge is possible. Keeps branch history explicit. |
| 30 | +- `--squash`: Combines all commits from the branch being merged into a single commit. Useful for keeping history cleaner. |
| 31 | +- `--abort`: Aborts a merge in progress and restores the branch to its pre-merge state. |
| 32 | + |
| 33 | +## How Git Merge Works |
| 34 | + |
| 35 | +The primary goal of `git merge` is to combine the histories of two branches in a way that preserves the integrity of all changes. To understand this process better, it helps to know what Git is doing behind the scenes. |
| 36 | + |
| 37 | +### Step 1: Identifying the Branches Involved |
| 38 | + |
| 39 | +Suppose you are on the `main` branch and want to merge changes from `feature-branch` into it. Git starts by identifying: |
| 40 | + |
| 41 | +- The current branch (where you want to apply the changes) — here, `main`. |
| 42 | +- The branch to merge — here, `feature-branch`. |
| 43 | + |
| 44 | +### Step 2: Finding the Merge Base |
| 45 | + |
| 46 | +In the next step, Git finds the merge base. The merge base is essentially the last commit from which both branches diverged. It acts as a reference point so Git can compare what changed on each branch since then. |
| 47 | + |
| 48 | +### Step 3: Determining the Merge Strategy |
| 49 | + |
| 50 | +Git chooses a merge strategy depending on the relationship between the current branch, the branch being merged, and their histories. |
| 51 | + |
| 52 | +Strategy 1: Fast-Forward Merge |
| 53 | + |
| 54 | +If the current branch (`main`) has no new commits after the merge base, Git can just "fast-forward" the branch pointer forward to the tip of `feature-branch`. |
| 55 | + |
| 56 | +No new commit is created here, and the history remains linear. |
| 57 | + |
| 58 | +Strategy 2: 3-Way Merge |
| 59 | + |
| 60 | +If both branches have diverged — meaning commits exist on both `main` and `feature-branch` since the merge base — Git performs a 3-way merge. |
| 61 | + |
| 62 | +Git compares: |
| 63 | + |
| 64 | +- The merge base commit |
| 65 | +- The HEAD of the current branch |
| 66 | +- The HEAD of the branch being merged |
| 67 | + |
| 68 | +Using these three points, Git figures out how the changes from each branch can be combined. |
| 69 | + |
| 70 | +It then creates a new commit called a merge commit that has two parent commits representing the integration of both histories. |
| 71 | + |
| 72 | +### Step 4: Applying Changes and Updating History |
| 73 | + |
| 74 | +During the merge process, Git: |
| 75 | + |
| 76 | +- Applies changes introduced by commits on the branch being merged that don’t exist on the current branch. |
| 77 | +- Preserves the commits and history of both branches. |
| 78 | +- Updates the current branch pointer to the new commit (either fast-forward or merge commit). |
| 79 | + |
| 80 | +## Example: Using Git Merge |
| 81 | + |
| 82 | +Let’s walk through a practical example to see how `git merge` works in action. |
| 83 | + |
| 84 | +**Scenario:** |
| 85 | + |
| 86 | +You are working on a project with two branches: |
| 87 | + |
| 88 | +- `main`: The stable production branch. |
| 89 | +- `feature-login`: A branch where you developed a new login feature. |
| 90 | + |
| 91 | +You want to merge the `feature-login` branch into `main`. To do so, follow these steps: |
| 92 | + |
| 93 | +Step 1: Switch to the target branch (`main`) |
| 94 | + |
| 95 | +```shell |
| 96 | +git switch main |
| 97 | +``` |
| 98 | + |
| 99 | +Step 2: Retrieve the latest changes from the [remote repository](https://www.codecademy.com/resources/docs/git/remotes) (`origin`) and update your local `main` |
| 100 | + |
| 101 | +```shell |
| 102 | +git pull origin main |
| 103 | +``` |
| 104 | + |
| 105 | +Step 3: Merge the `feature-login` branch into `main` |
| 106 | + |
| 107 | +```shell |
| 108 | +git merge feature-login |
| 109 | +``` |
| 110 | + |
| 111 | +## Merge Conflicts |
| 112 | + |
| 113 | +Sometimes, Git can’t automatically combine changes, especially when both branches modify the same lines of a file. This results in a merge conflict. |
| 114 | + |
| 115 | +To resolve: |
| 116 | + |
| 117 | +Step 1: Open the conflicting file(s) and decide how to combine changes |
16 | 118 |
|
17 |
| -## Git Command |
| 119 | +Step 2: Remove conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`) |
18 | 120 |
|
19 |
| -To merge a branch into the local `main` branch: |
| 121 | +Step 3: Stage the resolved file(s) |
20 | 122 |
|
21 | 123 | ```shell
|
22 |
| -git checkout main # Switch to the main branch |
23 |
| -git merge branch_name |
| 124 | +git add <file> |
24 | 125 | ```
|
25 | 126 |
|
26 |
| -For example, the following merges a branch called `feature_test` branch to the `main` branch: |
| 127 | +Step 4: Commit the merge |
27 | 128 |
|
28 | 129 | ```shell
|
29 |
| -git checkout main |
30 |
| -git merge feature_test |
| 130 | +git commit |
31 | 131 | ```
|
32 | 132 |
|
33 |
| -## Merging a Pull Request on GitHub |
| 133 | +## Best Practices for Using Git Merge |
34 | 134 |
|
35 |
| -> _For information on how to open a pull request, see the [Pull Requests](https://www.codecademy.com/resources/docs/git/pull-requests) section._ |
| 135 | +- Pull latest changes before merging to avoid outdated merges. |
| 136 | +- Use descriptive branch names for clarity in commit history. |
| 137 | +- Test your code after merging to catch any integration issues early. |
| 138 | +- Consider `--no-ff` to always create a merge commit for better history tracking. |
36 | 139 |
|
37 |
| -1. On the GitHub repository page, click the "Pull requests" tab at the top. |
| 140 | +## Frequently Asked Questions |
38 | 141 |
|
39 |
| -2. In the "Pull Requests" list, choose the pull request that you'd like to merge. |
| 142 | +### 1. What is a 3-way merge? |
40 | 143 |
|
41 |
| -3. Depending on the merge options enabled for your repository, you can: |
| 144 | +A 3-way merge occurs when Git uses: |
42 | 145 |
|
43 |
| - - Merge all the commits into the base branch by clicking **Merge pull request**. If the **Merge pull request** option is not shown, then click the merge drop down menu and select **Create a merge commit**. |
| 146 | +- The latest commit of the current branch (HEAD) |
| 147 | +- The latest commit of the branch to be merged |
| 148 | +- Their common ancestor |
44 | 149 |
|
45 |
| - - Squash the commits into one commit by clicking the merge drop down menu, selecting **Squash and merge** and then clicking the **Squash and merge** button. |
| 150 | +Git compares all three to create a new merge commit that combines the changes. |
46 | 151 |
|
47 |
| - - Rebase the commits individually onto the base branch by clicking the merge drop down menu, selecting **Rebase and merge** and then clicking the **Rebase and merge** button. |
| 152 | +### 2. What is `git merge` vs `git pull`? |
48 | 153 |
|
49 |
| -4. If prompted, type a commit message, or accept the default commit message. |
| 154 | +- `git merge` incorporates changes from one branch into your current branch (local operation). |
| 155 | +- `git pull` retrieves changes from a remote and then merges them into the current branch (remote + merge in one step). |
50 | 156 |
|
51 |
| -5. If you have more than one email address associated with your GitHub account, click the email address drop-down menu and select the email address to use as the Git author email address. Only verified email addresses appear in this drop-down menu. If you enabled email address privacy, then `<username>@users.noreply.github.com` is the default commit author email address. |
| 157 | +### 3. When should I use `git merge`? |
52 | 158 |
|
53 |
| -6. Click **Confirm merge**, **Confirm squash and merge**, or **Confirm rebase and merge**. |
| 159 | +Use `git merge` when you: |
54 | 160 |
|
55 |
| -7. (Optional) Last but not the least, delete the feature branch. This keeps the list of branches in your repository nice and tidy. |
| 161 | +- Want to combine changes from a completed feature branch into the main branch. |
| 162 | +- Need to integrate teammate changes into your branch without overwriting your own work. |
| 163 | +- Prefer to keep commit history intact instead of rewriting it (as in `git rebase`). |
0 commit comments