Git Merge branch to Master and Another branch.
To make a source code development in parallel. Git provides a way to create branches from the master or another branch. Once development completes, there is a need to merge branches into another branch.
This git tutorial describes how we can merge a branch into another branch. A Git merge command merges code changes from one branch to any other branch.
For simplicity, we have taken an example with no merge conflicts. Merge with conflicts; we will cover details in another tutorial.
Prerequisites for the GIT merge to master and another branch example:
- There is a git repository already created with the nameĀ
myrepository
. - The repository has branches named branch_1 and branch_2, along with the master.
- Both branches have been created from the master.
- There was no change in any of the branches after the creation of branches from the master.
- The Git repository has been cloned to the local Linux machine.
What will we show in the git merge command example?
- In the example, we will change the code in the branch_1 git branch.
- After changes, branch_1 will merge into the master.
- After master branch_1 will merge in branh_2
- We will verify the change after merging at each step.
List down the branches before merging:
Git provides a branch command to list all branches.
[root@CentOS_6_64-160 myrepository]# git branch branch_1 branch_2 * master [root@CentOS_6_64-160 myrepository]#
Above we can see two more branches other than the master. To show the example of git merge, first, we will make the changes in branh_1. To make changes, we will switch to a new branch, branch_1, using the git checkout command.
[root@CentOS_6_64-160 myrepository]# git checkout branch1
error: pathspec 'branch1' did not match any file(s) known to git.
[root@CentOS_6_64-160 myrepository]# git checkout branch_1
M firstFile.c
M secondFile.c
Switched to branch 'branch_1'
[root@CentOS_6_64-160 myrepository]# git branch
* branch_1
branch_2
master
[root@CentOS_6_64-160 myrepository]# ls -rlt
total 8
-rw-r--r-- 1 root root 3 Dec 23 03:37 firstFile.c
-rw-r--r-- 1 root root 9 Dec 23 03:37 secondFile.c
[root@CentOS_6_64-160 myrepository]#
Change the code in branh_1 before the merge :
The following example with command first will change the firstFile.c
. The example commits the changes to the git repository.
[root@CentOS_6_64-160 myrepository]# git add firstFile.c
[root@CentOS_6_64-160 myrepository]# git commit -m "modified branch_1"
[branch_1 e0d4d8a] modified branch_1
Committer: root <root@CentOS_6_64-160.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <you@example.com>'
1 files changed, 1 insertions(+), 0 deletions(-)
[root@CentOS_6_64-160 myrepository]# vim firstFile.c
Merge The branch_1 into the master.
[root@CentOS_6_64-160 myrepository]# git checkout master
[root@CentOS_6_64-160 myrepository]# git merge branch_1
Updating 439d0a7..e0d4d8a
Fast-forward
firstFile.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@CentOS_6_64-160 myrepository]# vim firstFile.c
Merge the brach_1 into branch_2
[root@CentOS_6_64-160 myrepository]# git checkout branch_2
M secondFile.c
Switched to branch 'branch_2'
[root@CentOS_6_64-160 myrepository]# git branch
branch_1
* branch_2
master
[root@CentOS_6_64-160 myrepository]# git merge branch_1
Updating 439d0a7..e0d4d8a
Fast-forward
firstFile.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@CentOS_6_64-160 myrepository]# vim firstFile.c
[root@CentOS_6_64-160 myrepository]#
How can we Verify the merge in the master and another branch?
The verification is straightforward. To verify the branch merge. We can switch to the master and open the file firstFile.c. This file should have the exact change we did in branch_1. Similarly, we can verify the same for another branch branch_2