Introduction

In our previous articles on git branches we explained what are branches in git terminology. Branches provide a means to have a working copy of our code available in the master branch while we continue to make changes to it in a different branch. We also practically demonstrated how to create branches and work with them. In this article we will demonstrate how we could merge the work or updates that we’ve made while working in a branch with the master branch. The demonstration will consist of merging changes with the master branch on the command line as well as on a remote. For the purpose of this demonstration we will be using GitHub as our remote since we’ve already set it up and synced our repositories up there.

Merging a git branch with the master branch on the command line:
To merge a branch we first need to move in to the branch that we want to incorporate changes into.

[sahil@linuxnix perl_scripts_for_training]$ git status
# On branch my_branch
nothing to commit, working directory clean
[sahil@linuxnix perl_scripts_for_training]$

The output of the git status command above tells us that we are in the branch my_branch at the moment. We want to merge the changes made while working in this branch with the master branch. So let’s move into the master branch using the git checkout command as shown below:

[sahil@linuxnix perl_scripts_for_training]$ git checkout master
Switched to branch 'master'
[sahil@linuxnix perl_scripts_for_training]$
[sahil@linuxnix perl_scripts_for_training]$ git status
# On branch master
nothing to commit, working directory clean
[sahil@linuxnix perl_scripts_for_training]$

We’ve now moved to the master branch and verified it with the git status command. Now we want to merge the changes made in the branch my_branch with the master branch. We accomplish this by using the git merge command as shown below:

[sahil@linuxnix perl_scripts_for_training]$ git merge my_branch
Updating e9bcebe..2d3806c
Fast-forward
hello.pl | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[sahil@linuxnix perl_scripts_for_training]$

The output of the git merge command is similar to that of git pull. It will show you all the files that were changed with the merge and also the number of lines that were modified. If we run the git log command now, it will show us that we’ve merged the changes from the branch my_branch in to the master branch and that now we are one commit ahead of the origin i.e. GitHub.

[sahil@linuxnix perl_scripts_for_training]$ git log --oneline --all --decorate --graph
* 2d3806c (HEAD, my_branch, master) updated hello.pl
* e9bcebe (origin/master) updated say.pl
|\
| * 75f54de updated say.pl
* | 28153ad updated say.pl
|/
* 357b280 updated say.pl
* 9181f1a updated say.pl
* 020e3e5 updated say.pl
|\
| * 05b5fed added a line to say.pl
* | 04a689d moddifed say.pl
|/
* 913ff69 resolved merge conflict in hello.pl
|\
| * 6e528de added the line My name is Sahil to hello.pl
* | 0cf7bb1 Modified the file hello.pl
|/
* d619327 Modified metadeta for this script say.pl
* 9e68102 add file testfile
* 7c57851 A repository of basic Perl scripts to be used as examples in training

Merging a git branch with the master branch on GitHub:
To demonstrate this let’s switch to the branch my_branch, make a change to a file a commit that change.

[sahil@linuxnix perl_scripts_for_training]$ git checkout my_branch
Switched to branch 'my_branch'
[sahil@linuxnix perl_scripts_for_training]$ ls -l hello.pl
-rwxrwxr-x 1 sahil sahil 264 Sep 10 19:35 hello.pl
[sahil@linuxnix perl_scripts_for_training]$ vim hello.pl
[sahil@linuxnix perl_scripts_for_training]$ git add .
[sahil@linuxnix perl_scripts_for_training]$ git commit -m "removed a line from hello.pl"
[my_branch 6f8586a] removed a line from hello.pl
1 file changed, 1 deletion(-)
[sahil@linuxnix perl_scripts_for_training]$ git status
# On branch my_branch
nothing to commit, working directory clean
[sahil@linuxnix perl_scripts_for_training]$

Up until now whenever we’ve used git push we’ve used the master branch. This time we will use the branch that we are currently working with i.e. my_branch and push our changes out to GitHub.

[sahil@linuxnix perl_scripts_for_training]$ git push origin my_branch
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 301 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To git@github.com:sahilsuri007/perl_scripts_for_training.git
* [new branch] my_branch -> my_branch
[sahil@linuxnix perl_scripts_for_training]$

As you can see from the above output a new branch with the name my_branch was created on GitHub. The new branch will now appear on GitHub as well as shown in the screen shot below:

Here you can see a button named “Compare and pull request”. Click on it. Now you will be presented with the below page. Since a merge is similar to a commit in the sense that we are modifying content and saving it, you can add a commit message by modifying the existing commit message being displayed or leave it as is as I’ve done here.

Click on “Create pull request”. Now from here you’ll see the below screen.

Click on the “Merge pull request” button. You’ll be asked to confirm the merge. Click on the “confirm merge” button to merge the updates from the branch my_branch with the master branch.

Conclusion

This concludes our discussion on merging changes in different branches on the command line and the GitHub interface. We hope that you found this article to be useful and we look forward towards your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.