views:

58

answers:

3

I find myself referring to the root commit for various reasons. I usually make a tag called first or root to make it easier, but I have been dealing with quite a few repositories lately and even this manual tagging is getting quite tedious.

Is there an easy way to refer to the root commit, something similar to HEAD or FETCH_HEAD?

+2  A: 

I only see tagging that first commit relevant for you as the only current solution.
That way, you can refer to it through a well named tag instead of looking for its SHA1 through the log.

Note: there can be several "root" commits in a Git repo (i.e. several commits without any parent)
See for instance this thread, with Jakub Narębski's answer:

Not to mention that you can have multiple roots (multiple commits with no parent) in git repository.
Besides independent branches (like 'man', 'html' or 'todo') it is usually result of absorbing or subtree-merging other projects.
In 'master' branch there are 5 roots or more: joined 'git-tools' (mailinfo / mailsplit), absorbed gitweb, and subtree-merged gitk and git-gui.

VonC
A: 

Not so easy, but you can get the sha of the "root" that corresponds to HEAD (head's parent's parent's parent's ... parent until a commit is reached that has no parent) using

git rev-list --topo-order --reverse HEAD | head -n 1

Works for any commit actually. If there are multiple convergent paths of history, you'll get the root of the path that has the most commits in it.

I can't say that I see any use whatsoever for it, though. Most things that have any reason to traverse history an unlimited depth into the past already know how.

hobbs
Note also that there might be more than one root (parentless) commit in a git repository; the above invocation would find one of them.
Jakub Narębski
+1  A: 
Jakub Narębski