This is a continuation of the previous post. This post will deal with branching, merging, and viewing commits.

Though these commands are not absolutely necessary to work with git, they are pretty useful and make the overall experience of using git safer and easier.

\(\\\)

Viewing Commits

You might want to view your commit history for several reasons. The commit history allows you to check who made a particular commit, when it was made and what the commit message was.

The command is aptly named: git log. Using the standard command without any additional tags allows you to view your commit history as already mentioned

Lets create a test repo named gitpractise. Inside we’ll create the file gitlogpract.txt. Next, we’ll make a few commits.

:~$ mkdir gitpractise
:~$ cd gitpractise

:~$ cat > gitlogpract.txt
This is the original file

:~$ git init
:~$ git add gitlogpract.txt

:~$ git commit -m "Commit 1"
[master (root-commit) c95cc9d] Commit 1
 1 file changed, 1 insertion(+)
 create mode 100644 gitlogpract.txt

:~$ cat >> gitlogpract.txt
This is the 1st change
:~$ git add -A

:~$	git commit -m "Commit 2"
[master e698c9d] Commit 2
 1 file changed, 1 insertion(+)
 
:~$ git log
commit e698c9d6f65a7a1eb7c1a8ae54d5de6decce2fe5 (HEAD -> master)
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:30:33 2018 +0530

    Commit 2

commit c95cc9d79b6b80999cc4b841ae39dd458c403922
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:29:29 2018 +0530

    Commit 1

If you observe the output of the git log command, you’ll see that we got the Author, the date and time, the commit ID and the commit message. The commit ID is in SHA -1 form.

Of course, there are several tags available for this command.

\[\\\]

git log –oneline

Shows a more compact representation

:~$ git log --oneline
e698c9d (HEAD -> master) Commit 2
c95cc9d Commit 1

git log –stat

Gives a more detailed information which includes which files and lines were modified

:~$ git log --stat
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:30:33 2018 +0530

    Commit 2

 gitlogpract.txt | 1 +
 1 file changed, 1 insertion(+)

commit c95cc9d79b6b80999cc4b841ae39dd458c403922
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:29:29 2018 +0530

    Commit 1

 gitlogpract.txt | 1 +
 1 file changed, 1 insertion(+)

git log -p

Most detailed view of a commit which includes a diff showing the changes made with that commit.

:~$ git log -p
commit e698c9d6f65a7a1eb7c1a8ae54d5de6decce2fe5 (HEAD -> master)
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:30:33 2018 +0530

    Commit 2

diff --git a/gitlogpract.txt b/gitlogpract.txt
index b9f618a..b7efcd9 100644
--- a/gitlogpract.txt
+++ b/gitlogpract.txt
@@ -1 +1,2 @@
 This is the original file
+This is the 1st change

commit c95cc9d79b6b80999cc4b841ae39dd458c403922
Author: Aniruddha Karajgi <akarajgi0@gmail.com>
Date:   Fri Oct 19 17:29:29 2018 +0530

    Commit 1

diff --git a/gitlogpract.txt b/gitlogpract.txt
new file mode 100644
index 0000000..b9f618a
--- /dev/null
+++ b/gitlogpract.txt
@@ -0,0 +1 @@
+This is the original file

git log –graph

This is a handy command which creates a graph showing the commit tree. It is difficult to appreciate this command in a small project without any pull requests or branches.

:~$ git log --graph
* commit e698c9d6f65a7a1eb7c1a8ae54d5de6decce2fe5 (HEAD -> master)
| Author: Aniruddha Karajgi <akarajgi0@gmail.com>
| Date:   Fri Oct 19 17:30:33 2018 +0530
| 
|     Commit 2
| 
* commit c95cc9d79b6b80999cc4b841ae39dd458c403922
  Author: Aniruddha Karajgi <akarajgi0@gmail.com>
  Date:   Fri Oct 19 17:29:29 2018 +0530
  
      Commit 1

\(\\\)

Branches

One of the strengths of git is the efficiency with which branches work in comparison to other version control systems. Branches allow you to work independently on an unstable part of the project without actually disturbing the main stable project. Because of their efficiency, branches are frequently used and recommended, especially in large opensource projects where one can’t afford to bring down the whole project such because of a small bug in an additional feature being worked on. Once the additional part of the project is deemed stable, it can be seemlessly merged with the main project or “master branch”.

In our ongoing project, lets create a “test branch”. Next we’ll modify the gitlogpract.txt file while we are in the test branch.

git branch

This command lists the existing branches in our git repo.

:~$ git branch
* master

We are currently on the master branch, which is the only branch in our project. The \(*\) indicates the current branch.

git branch [branch_name]

Now we create the test branch. We check to see if the branch has been created as expected.

~:$ git branch test
~:$ git branch
* master
  test

git checkout [branch_name]

Our next step would be to move to the test branch. Using the checkout command:

:~$ git checkout test
Switched to branch 'test'
:~$ git branch
  master
* test

Notice how the $ * $ has moved to the test branch.

Nows lets make a change to the gitlogpract.txt file.

:~$ cat >> gitlogpract.txt 
This is the change made in test branch
:~$ git add -A
:~$ git commit -m "Commit 3 test branch"
[test 14a1d13] Commit 3 test branch
 1 file changed, 1 insertion(+)
 
:~$ cat gitlogpract.txt 
This is the original file
This is the 1st change
This is the change made in test branch

Now, lets go back to the master branch and check the contents of gitlogpract.txt.

:~$ git checkout master
Switched to branch 'master'

:~$ cat gitlogpract.txt 
This is the original file
This is the 1st change

Clearly, the change we just made in the test branch is not reflected in the master branch.

git merge [branch_name]

Now, suppose you are satisfied with the changes you have made in the test branch and you want those changes to reflect in the main project (the master branch) The “HEAD” or the branch we are currently in must be that in which we want to merge the changes from another branch. In this case, we must be in the master branch.

:~$ git merge test
Updating e698c9d..14a1d13
Fast-forward
 gitlogpract.txt | 1 +
 1 file changed, 1 insertion(+)

When we check the contents of gitlogpract.txt, we find that they reflect the changes we made in the test branch before we merged it with the master branch.

:~$ cat gitlogpract.txt
This is the original file
This is the 1st change
This is the change made in test branch

git branch -d [branchname]

If our created branch serves no purpose, we can delete it by using the above command.

:~$ git branch -d test
Deleted branch test (was 14a1d13).
:~$ git branch
* master

The git branch command shows us that only the master branch remains.

The basics of viewing the commit history, branches and merging have been convered here.