How to View Logs of a Particular Branch in Git

Azhar Bashir Khan Feb 02, 2024
How to View Logs of a Particular Branch in Git

This tutorial will teach how to view the logs of a particular branch in the Git repository.

Git, a version control system, tracks changes in a project directory. Git uses commits for such purposes.

Typically, different branches are created in a Git repository to track each different development work. We may thus wish to view the logs of a particular branch only to view the changes in that branch.

We can use the git log command to view the logs of the branches in the Git repository. We will illustrate this with an example.

View Logs of a Particular Branch in Git

Git is used in a collaborative development environment to keep track of the modifications done to the files in the project directory. In the collaborative development environment, typically, different branches are created to track the different development efforts.

We may create one branch to track the front-end development effort in the Git repository. One for the backend work, one for testing, and so on.

We may then wish to view logs of only a particular branch in the Git repository. We can check the changes in a particular branch by viewing those logs and the commits.

Suppose we have a branch named frontend to track the front-end development effort in our Git repository. We can use the git log command to view the branch frontend logs.

$ git log frontend --oneline
3c39d7b (origin/frontend, frontend) merged from main
9c87339 added README.md
d40928b Merge branch 'main' of github.com:johndoe/MyProject into main
0fd1782 Initial commit
1fd51f3 first MyProject commit

The listed logs contain the logs of the main branch. The main branch is the remote branch merged into the frontend branch.

To view only the frontend branch logs, excluding the logs present in other branches, we need to execute the git log command.

$ git log main..frontend --oneline
3c39d7b (origin/frontend, frontend) merged from main
9c87339 added README.md

The logs shown are the branch frontend’s commits, excluding the commits reachable by the other branches, viz., the main branch.

The equivalent git log command to the one given above to view only the frontend branch logs is as follows.

$ git log frontend ^main --oneline
3c39d7b (origin/master, master) merged from main
9c87339 added README.md

One can think of the listing of commits by the git log command as a set operation. The commits that are reachable by any of the commits given on the command line form one set, viz. frontend.

The commits that are reachable from the ones given by ^ in front of them are then subtracted from the set. Then, the resultant commits are listed in the git log command output.

Thus, we have learned how to view the logs of only a particular branch in the Git repository.

For more information, please visit:

  1. git-log
  2. Advanced Git log

Related Article - Git Log