Introduction

In our previous article, we introduced you to the concept of branches in git. We explained what are branches and demonstrated how we could create a branch, switch to a different branch and delete a branch. In this article we will demonstrate how we use branches to prototype our work while keeping a functioning copy of our work available in the master branch.

Demonstration
Let’s start by creating a new branch and then move in to that branch.

[sahil@linuxnix perl_scripts_for_training]$ git checkout -b my_branch
Switched to a new branch 'my_branch'
[sahil@linuxnix perl_scripts_for_training]$

Now we will make a commit on this branch by modifying the perl script hello.pl.

[sahil@linuxnix perl_scripts_for_training]$ cat hello.pl
#!/usr/bin/perl -w

######################################
#Author: Sahil Suri
#Date: 24/03/2018
#Purpose: Hello World in Perl
#version: v1.0
######################################

print "Hello World\n";

print "Hello World again.\nMy name is Sahil.\n"
[sahil@linuxnix perl_scripts_for_training]$

I’ve updated the script such that it has the below content now:

[sahil@linuxnix perl_scripts_for_training]$ cat hello.pl
#!/usr/bin/perl -w

######################################
#Author: Sahil Suri
#Date: 24/03/2018
#Purpose: Hello World in Perl
#version: v1.0
######################################

print "Hello World\n";

print "Hello World again.\n"
[sahil@linuxnix perl_scripts_for_training]$

Now we will add and commit these changes.

[sahil@linuxnix perl_scripts_for_training]$ git add .
[sahil@linuxnix perl_scripts_for_training]$ git commit -m 'updated hello.pl'
[my_branch 2d3806c] updated hello.pl
1 file changed, 1 insertion(+), 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]$

From the above git status output we can ascertain that our commit has been made successfully and that we are on the branch my_branch and not the master branch. Now let’s move back to the master branch.

[sahil@linuxnix perl_scripts_for_training]$ git checkout master
Switched to branch 'master'

Now if take a look at the contents of the file hello.pl we will find that the contents do not contain the update that we had made.

[sahil@linuxnix perl_scripts_for_training]$ cat hello.pl
#!/usr/bin/perl -w

######################################
#Author: Sahil Suri
#Date: 24/03/2018
#Purpose: Hello World in Perl
#version: v1.0
######################################

print "Hello World\n";

print "Hello World again.\nMy name is Sahil.\n"
[sahil@linuxnix perl_scripts_for_training]$

This shows that we can continue to prototype and test code in a different branch while keeping a working copy of the code intact in the master branch.

While working with branches the git log will show the log only for the current branch. I’ve executed the git log command here while I was in the master branch.

[sahil@linuxnix perl_scripts_for_training]$ git log --oneline
e9bcebe updated say.pl
28153ad updated say.pl
357b280 updated say.pl
9181f1a updated say.pl
020e3e5 updated say.pl
04a689d moddifed say.pl
75f54de updated say.pl
05b5fed added a line to say.pl
913ff69 resolved merge conflict in hello.pl
0cf7bb1 Modified the file hello.pl
6e528de added the line My name is Sahil to 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
[sahil@linuxnix perl_scripts_for_training]$

As you may have observed a log of the commit we made while in my_branch is not available. Now when I move to the branch my_branch using the git checkout command and then run the git log command again, we will be able to see the log of the commit made while working in this branch.

[sahil@linuxnix perl_scripts_for_training]$ git checkout my_branch
Switched to branch 'my_branch'
[sahil@linuxnix perl_scripts_for_training]$ git log --oneline
2d3806c updated hello.pl
e9bcebe updated say.pl
28153ad updated say.pl
357b280 updated say.pl
9181f1a updated say.pl
020e3e5 updated say.pl
04a689d moddifed say.pl
75f54de updated say.pl
05b5fed added a line to say.pl
913ff69 resolved merge conflict in hello.pl
0cf7bb1 Modified the file hello.pl
6e528de added the line My name is Sahil to 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

We can use the git log command with the –all option which will show us the commit logs from all the branches but it will not be very easy to interpret since we do not know which branch a commit message belongs to. For this the git log provides two more flags which we may make use of. These options are –graph and –decorate. When we use the git log command with these options it shows us the branches to which a commit belongs to and also the currently checked out branch as well. Given below is an example:

[sahil@linuxnix perl_scripts_for_training]$ git log --oneline --all --graph --decorate
* 2d3806c (HEAD, my_branch) updated hello.pl
* e9bcebe (origin/master, 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
[sahil@linuxnix perl_scripts_for_training]$

Conclusion

This concludes our demonstration on working with git branches wherein we explained how we could continue to work on files in different branches and make commits while keeping a copy of our data isolated within the master branch.

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.