Search code examples
gitgit-hash

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


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


Solution

  • 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.