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 in another branch. This git tutorial describes, how we can merge a git branch into master or in any other branch. Git merge command can merge code changes from a 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 two 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.
  • Git repository has been cloned to the local Linux machine.

What we will show in git merge command example?

  • The example, we will make the code changes in the branch_1 git branch.
  • After changes branch_1 will merge in 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 merge:

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 that there are two more branches other than 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 branh_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 :

Following command example, 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 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 we can Verify the merge in master and another branch?

The verification is very simple. To verify the branch merge.  We can switch to the master and open the file firstFile.c. This file should have the same change, we did in branch_1. In a similar way, we can verify same for another branch branch_2

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top