0

Suppose I checkout an older commit, with commit sha/id: commit_34 of branch branch_a of repository repo_b, with:

git checkout commit_34

Then, the old commit sha (which is the commit sha of the new head) can be retrieved with:

git rev-parse HEAD

which returns:

commit_34

Once in this commit (with sha commit_34), without checking out another commit, how could I get the latest (in absolute sense e.g. commit_36) commit sha in that branch branch_a?

Example

Suppose the commit sha's in branch branch_a are, from latest on top, to oldest on bottom:

commit_36
asdlasdfalsdfhlaksdhflkajsdfhalskdjfh3243
commit_34
asdlasdfalsdfhlakssdfsdf1sdfhalskdjfh3243
asdfljahslvkhasdljfkh324klj2h34lkj234hh3j
...

Assumption

I (currently) do not know what the current branch is. I will look into how I can retrieve that. Such that I can apply the answers given in this post.

a.t.
  • 1,411
  • 1
  • 15
  • 49
  • 2
    In git, commits aren't "on a branch", because history is traced _backwards_: a branch points to its latest commit, which points to a parent (or multiple parents for a merge, or none at the very beginning of the history), and so on through history. So, given only a commit hash you can only look for _all branches that have the commit somewhere in their history_. However, if you want the branch which you _asked to checkout_, that is tracked, see https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git – IMSoP Mar 03 '22 at 22:10

0 Answers0