tags:

views:

459

answers:

2

I've got a script that needs to reference the initial commit in a repository. git has the special reference HEAD, but doesn't have the corresponding TAIL. I cannot find anything in git help rev-parse that would seem to help me.

Here's what I'd like to do:

git show TAIL

Here's one option I have:

git show `git log --reverse | if read a commit ; then echo $commit ; fi`

That's pretty hacky and depends on the output of git log not changing.

Right now I just tag the initial commit and use that as my refspec. However, I'd like to release a general tool, so that's not a great option.

Thanks for any help

+4  A: 

git rev-list HEAD | tail -n 1 is a more stable option.

Robert Munteanu
This would return *one* of tail commits; there can be more than one root (parentless) commit
Jakub Narębski
+9  A: 

Do not use git-log for scripting: use either git-rev-list, or git-log with specified custom format ("--format=<sth>" option).

There is additional problem with your question: there can exist more than one such TAIL root commit (parentless commit) in a repository (even if we discount disconnected branches, such as 'html', 'man' and 'todo' in git.git repository). This is usually result of joining separate projects in one, or using subtree merge of separately developed subproject.

For example git repository has 6 root commits: git-gui, gitk (subtree-merged), gitweb (merged in, no longer developed separately), git mail tools (merged very early in project history), and p4-fast-export (perhaps accidental). That is not counting roots of 'html and 'man' branches, "convenience" branches which contains pre-generated documentation, and 'todo' branch with TODO list and scripts.


You can get list of all parentless (root) commits accessible from current branch using:

$ git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"

Proposed by Robert Munteanu solution

$ git rev-list HEAD | tail -n 1

would return one of those root (tail) commits.

Jakub Narębski