Merge Operation Using GitHub
How to merge branches through the command line on the local computer was already explained in the previous chapter. In this section, we'll explain how to merge branches on the GitHub web platform.
The merge feature on GitHub gives similar functionality as the Git commands below. There are three merge approaches which are a combination of the three Git commands: the git merge
, git rebase
, and git commit
commands.
- Create a merge commit:
git merge --no-ff
- Squash and merge:
git merge --squash
+git commit
- Rebase and merge:
git rebase
+git merge
1. Create a merge commit
This approach is the one most commonly used. According to this approach, the base branch captures and keeps all the commit histories from the topic branch as shown in the illustration on the left of the main figure.
2. Squash and merge
When you want to integrate all the small commits to simplify the commit history of the project, you can use this option. A drawback of this option is that all the commits of the topic branch are erased from the history as shown in the middle illustration on the main figure.
3. Rebase and merge
If you have many branches in the project and merge them into the master branch, the commit history of the project becomes very complex. This option is used to make the commit line straight as shown the illustration on the right of the main figure.
For a better understanding, please go through the following practice section.
Practice
Objective:
Learn the merge operations on the GitHub web platform
In this practice, we'll go through the three merge approaches on GitHub using the branches prepared on the previous pages. The target operations are illustrated below.
- Create a merge commit: merge Branch_C into the master branch
- Squash and merge: squash and merge Branch_A
- Rebase and merge: rebase and merge Branch_B
1. Create a merge commit: merge Branch_C into the master branch
On the previous page, we have already explained about the pull request process until a reviewer approves the changes (Developer A already approved Developer B's code).
The merge operation can be done by either party: the requester or the approver of the pull request. In this step, we'll explain the case where the reviewer (Developer A) conducts the merge operation.
Action by Developer A
1) To execute the merge operation, go to the pull request page created on the previous page. Select Create a merge commit in the Merge pull request pull-down and click the button again.
2) Press the Confirm merge button to execute.
3) A confirmation message is displayed:
4) Check the commit history on the master branch. Go to the Code tab and open the commits.
You will see that Branch_C was integrated into the master branch while the commits made on Branch_C were kept unchanged.
2. Squash and merge: squash and merge Branch_A
In this step, we'll continue to execute the merge operation with the Developer A account. When you want to execute the merge operation by yourself, you can create a pull request on your own and execute the merge operation.
Action by Developer A
1) Create a new pull request from the Pull requests tab and press the New pull requests button.
2) Select Branch_A to compare, and press the Create pull request button
3) Add a PR comment and press the Create pull request button. As Developer A executes the merge operation himself, you don't need to select a reviewer.
4) Select Squash and merge under the Merge pull request pull-down.
5) Press the Confirm squash and merge button. The squash merge is executed.
6) Check the commit history on the master branch. You can see that Branch_A was squashed and merged. As the original commits are squashed, you cannot see the original commit details (A1LA, A2LA).
3. Rebase and merge: rebase and merge Branch_B
Finally, execute rebase and merge Branch_B. We continue the operation using Developer A's account.
Action by Developer A
1) Follow the same process as in the previous step until the merge option selection (creating a pull request comparing the master branch with Branch_B).
For the merge operation, select Rebase and merge in the merge option list.
2) Press the Confirm rebase and merge button. Rebase and merge are executed.
3) Check the commit history on the master branch. You can see that Branch_B was rebased and merged – commit B1LA became the HEAD of the master branch.
4. Update the local branches
All the operations on this page were made on the GitHub platform through the web browser. The changes have not been synchronized in the Local Repositories. In this step, synchronize the changes made on the Remote Repository with Developer A and Developer B's Local Repositories.
Action by Developer A
First, check the latest status of the master branch. You can see that the branch has not been updated yet.
git checkout master
git log --oneline
4e7dbf9 (HEAD -> master, origin/master) M3LA
4e1a7d9 M2RA
4b30a1c M1LA
Run the pull command and check the log with the --graph
option to see the latest commit tree.
git pull origin master
git log --oneline --graph
* 231b752 (HEAD -> master, origin/master) B1LA
* 6ae3208 Branch a (#2)
* 542b56b Merge pull request #1 from bloovee/Branch_C
|\
| * 3d69f7a C4LB
| * 6fc9649 C3LB
| * c33e4e8 C2LB
| * 7510fae C1LB
|/
* 4e7dbf9 M3LA
* 4e1a7d9 M2RA
* 4b30a1c M1LA
Repeat the same process for Developer B.
Action by Developer B
First, check the latest status of the master branch. You can see that the branch has not been updated yet.
git checkout master
git log --oneline
4e7dbf9 (HEAD -> master, origin/master, origin/HEAD) M3LA
4e1a7d9 M2RA
4b30a1c M1LA
Run the pull command and check the log with the --graph
option to see the latest commit tree.
git pull origin master
git log --oneline --graph
* 231b752 (HEAD -> master, origin/master, origin/HEAD) B1LA
* 6ae3208 Branch a (#2)
* 542b56b Merge pull request #1 from bloovee/Branch_C
|\
| * 3d69f7a (origin/Branch_C, Branch_C) C4LB
| * 6fc9649 C3LB
| * c33e4e8 C2LB
| * 7510fae C1LB
|/
* 4e7dbf9 M3LA
* 4e1a7d9 M2RA
* 4b30a1c M1LA
The demo code is available in this repository (Demo Code).