Git

List Commits Between 2 Commit Hashes in Git

While working on a development project in Git, developers make a lot of modifications to their projects. All of these changes or modifications are stored in the Git history. However, sometimes, the commit history contains so many commits that it becomes so complex to find particular commits. In this situation, Git allows users to list the specific commits between two commit hashes.

This write-up will explain the methods to display the commits between two commit hashes in Git.

How to List/Display Commits Between Two Commit Hashes in Git?

Different Git commands can be used to display the commits between two commit hashes, such as:

  • git log –oneline <commit-hash1>~…<commit-hash-2>
  • git rev-list –ancestry-path <comit-hash1>~…<commit-hash-2>

Method 1: List Commits Between Two Commit Hashes Using “git log –oneline” Command

To list commits between two commit hashes including the commit message, utilize the following command along with the desired commit hashes. For instance, we want to display the commits between the “60f911d” and “dc1157a” commit hashes:

git log --oneline 60f911d~...dc1157a

Here, the “~” symbol is used to exclude the “60f911d” commit.

The below output displayed the commits between the specific commit ids:

Moreover, if you want only to view the commit id between the two specified commit hashes, use the “cut -d ” ” -f 1” option with the same command:

git log --oneline 60f911d~...dc1157a | cut -d " " -f 1

Method 2: List Commits Between Two Commit Hashes Using “git rev-list” Command

Type out the following command along with the “–ancestry-path” option and specify the commit hashes to view the commits between them:

git rev-list --ancestry-path 60f911d~...dc1157a

The below image shows the full SHA-hash value of the commits between the specified commit ids:

We have explained the methods of listing the commits between two commit hashes in Git.

Conclusion

Various Git commands can be used to list the commits between two commit hashes, such as the “git log –oneline <commit-hash1>~…<commit-hash-2>” command displays the commits with commits messages and the “cut -d ” ” -f 1” option with the same command displays only the commit hashes. Moreover, the “git rev-list –ancestry-path <commit-hash1>~…<commit-hash-2>” command is used to display the full SHA hash of the commits between the specified commit ids. This write-up explained the methods to display the commits between two commit hashes in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.