1

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

Denis
  • 33
  • 1
  • 7
  • You mean like `git log master` with a custom format that shows only the hash? I didn't quite understand whether your attempt shows too little or too much. – mkrieger1 Dec 27 '21 at 16:55
  • And what do you mean by "a master" branch? Do you have multiple master branches? – mkrieger1 Dec 27 '21 at 16:56
  • only have one origin/master. `git log origin/master` shows logs of branches too. `git rev-parse origin/master --all` return commit IDs which is enough info. However for example the previous commit ID of the master branch is not in the result list. The commit ID of the previous master is different on GitLab UI. – Denis Dec 27 '21 at 17:03
  • 1
    `git rev-list --first-parent master` – jthill Dec 27 '21 at 17:29

3 Answers3

4

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.

Schwern
  • 153,029
  • 25
  • 195
  • 336
3

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.

knittl
  • 246,190
  • 53
  • 318
  • 364
2

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

git log --pretty=%H master
match
  • 10,388
  • 3
  • 23
  • 41
  • in my case, this also shows commit hash IDs of branches too. – Denis Dec 27 '21 at 17:05
  • 2
    A commit can be in both `master` and a branch, since branches originate in `master`, and can then be merged back in. – match Dec 27 '21 at 17:08