How to get all Git commit hash IDs of a master branch

4.6k Views Asked by At

I want to get all commit hash IDs of a master branch. Using the following command however only the latest commit id matches with the result. Old commit hash IDs don't belong to the master branch. Is there any other way to get commit hash IDs only belong to the master branch?

git rev-parse origin/master --all

3

There are 3 best solutions below

1
On BEST ANSWER

The OP commented...

in my case, this also shows commit hash IDs of branches too

tl;dr Use git rev-list master --first-parent.

In Git, commits do not belong to a branch. Commits connect to each other. A branch is merely a label pointing at the latest commit for that branch.

Instead, commits are reachable from a branch.

For example, say you have master and feature branched off master. It looks like this.

A - B - C - D - E [master]
         \
          F - G - H [feature]

master is just a label pointing at commit E. feature is just a label pointing at commit H.

If you git log master you'll get A, B, C, D, E; they are reachable from master. If you git log feature you'll get A, B, C, F, G, H; they are reachable from feature.

If you want to see just the commits reachable feature, without those shared with master, use git log master..feature. That will show (A, B, C, F, G, H) - (A, B, C, D, E) or F, G, H.

See gitrevisions for more.


After you merge feature into master and delete feature it looks like this.

$ git checkout master
$ git merge feature
$ git branch -d feature

A - B - C - D - E - M [master]
         \         /
          F - G - H

If you git log master you'll get A, B, C, D, E, F, G, H, M; everything reachable from master no matter what branch it was originally committed to.

You can see these connections and labels with git log --graph --decorate.


But we can get what are probably the original commits to master.

In the example above, the merge commit M connects to two commits, it has two "parents": E and H. These have an order. The first will be the branch which was merged into (master), and the second will be the branch which was merged in (feature).

If you only follow the first parent commit you'll get only the commits to master: A, B, C, D, E, M. Do this with --first-parent. Both git-log and git rev-list take --first-parent.

git log --first-parent --oneline master

Or

git rev-list --first-parent master

It's not guaranteed that this will produce the original commits to master, but if you're following a central branch workflow where branches are merged into master it will. More complex workflows may change the order of the parents.

2
On

You can get just the hashes for a particular branch (e.g. master) by running:

git log --pretty=%H master
0
On

A "branch" in Git only points to a (single!) commit and is moved forward automatically when new commits are made. A commit can point to 0, 1, 2 or even more parent commits. A commit is said to be "on a branch" if it is reachable from the tip or head of the branch. This means that once a branch is merged into another branch, it becomes part of that branch.

To get a list of all commit ids of a branch, you can use git rev-list branchname. Of course, this will also include commits that were merged ino from other branches. If you always merge in the same direction, you might say that only the first parent of each commit is important and relevant to you. In that case, you can filter the revision list to show only commits reachable through the first parent commit. To do so, specify the --first-parent flag to above command.