How to Show Commit History for One Branch Using Git Log With Range

Ankit Raj Goyal Feb 02, 2024
  1. Git Commit History for Branch Using git log and Advanced Limiting Options
  2. Git Commit History for Branch Using the Double Dot Syntax <branch1>..<branch2>
  3. Useful Shortcut With git log to Type a Few Characters Less to Show Git Commit History for Branch
  4. Helpful Option to See Only the Last Few Commits With log to Git Show Commits on Current Branch Only
  5. Git Commit History for Branch With the Caret Operator ^
  6. Git Commit History for Branch With the --not Option
  7. Resources
How to Show Commit History for One Branch Using Git Log With Range

The git log has many limiting options to obtain a subset of commits from your repository history. We demonstrate how git log with range using double-dot syntax, caret operator ^, --not option, and several shortcuts help us view git commit history for branch only.

Git draws its power from this rich revision query system that enables users to manage their branches and devise efficient workflows with them.

Git Commit History for Branch Using git log and Advanced Limiting Options

Git repositories can grow big with long commit histories. Viewing the full history is not very helpful in such cases.

The git log command provides us with many useful options to extract just the subset of commits from repository history that we need.

git log [formatting options] [limiting options]

The [formatting options] let us display different information about our commits, while [limiting options] help us select the commits we want to see.

When we provide the right range values in these limiting options, we can git show commits on the current branch.

Git Commit History for Branch Using the Double Dot Syntax <branch1>..<branch2>

The double-dot syntax shows those commits that we can reach from branch2 but not branch1.

Suppose we create a new experiment_branch from master. Then, we commit to both these branches but do not merge yet.

If we now do a bare git log on experiment_branch, it shows the full history, including the commits we made to master even before creating experiment_branch.

commits on master before experiment

This is not very helpful behavior. One common use case is when we want to see only the unmerged commits on experiment_branch.

We do so with the following command.

git log master..experiment_branch

All the commits are reachable from experiment_branch but not master (essentially, all the unmerged commits on the experimental branch).

git commit history for branch doubledot syntax

We could also flip the order of the branches for the reverse case to see the state of the master into which we are going to merge our experiment_branch.

Our master looks like this:

master new commits after experiment

When we do:

git log experiment_branch..master

We see only the new commits on master after creating our experimental branch.

new commits - master branch double dot

Useful Shortcut With git log to Type a Few Characters Less to Show Git Commit History for Branch

If we do not provide either of the two branches in the double-dot syntax, Git defaults them to the HEAD.

We can use this with a smart choice of git checkout to shorten our git log command.

First, we switch to our experimental branch.

git checkout experiment_branch

Now the HEAD points to experiment_branch.

We now use the short-form version of the double dot range syntax, omitting the right branch operand.

git log master..

Git defaults the right branch to the current HEAD, our experiment_branch, and we get the same history of only the unmerged commits as above.

git short form double dot

Helpful Option to See Only the Last Few Commits With log to Git Show Commits on Current Branch Only

Our experiment_branch might have a long history with many unmerged commits.

experiment long history many unmerged commits

We may want to see only the last few commits in such a case.

git log -2 master..

The -2 option shows up only the last 2 commits from our selected subset of the history.

show only last few commits in history

Git Commit History for Branch With the Caret Operator ^

The caret operator ^ excludes all the commits reachable from the branch it operates on. We can use this to show only the history of our current branch.

git log ^master experiment_branch

show commit history for branch caret

Git Commit History for Branch With the --not Option

We can also achieve the same result using the --not option before a branch name. This excludes all the commits reachable from that branch.

git log experiment_branch --not master

We see it does the same thing and only shows the unmerged commits on our experiment_branch.

show commit history for branch not option

Resources

  1. https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
  2. https://rakhesh.com/coding/git-view-the-commit-log-of-a-remote-branch/
  3. https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
  4. https://stackoverflow.com/questions/14889017/how-to-show-local-branch-history#14889649
  5. https://stackoverflow.com/questions/16974204/how-to-get-commit-history-for-just-one-branch

Related Article - Git Commit