166

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46

6 Answers6

184

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.


If you have git 1.7.4.2 or newer, you can use the --max-parents option:

$ git rev-list --max-parents=0 HEAD

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

$ git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"
papanito
  • 2,349
  • 2
  • 32
  • 60
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • For me, the `git rev-list HEAD | tail -n 1` and `git rev-list --max-parents=0 HEAD` are not returning the same hash value for me. The one using the `--max-parents=0` is in fact getting the initial commit though. Just thought I would point out that the latter seems more reliable. – jbranchaud Jan 23 '13 at 21:42
  • 4
    @Treebranch this could happen if you have commits with odd timestamps. adding `--topo-order` to the `rev-list` should fix that, though I think the `--max=parents=0` answer is best. – jthill May 11 '13 at 20:49
44

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

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
12

Not sure what you're trying to do here, but some git commands take --root as an option, in order to reference the root of the commit tree, e.g.

git rebase --interactive --root main
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
7

Assuming the first parent of a merge is the main line of development:

git rev-list --reverse --topo-order --first-parent HEAD | sed 1q
abh
  • 71
  • 1
  • 1
  • If i could award bonus points for using `sed` instead of `head` then I would. `sed 1q` can be pointlessly shortened to `sed q` by the way. – David Jones Jun 16 '21 at 17:13
2

Taken into account there could be multiple initial (orphan) but those commits still reachable from any of the refs it's possible to use:

git rev-list --all --max-parents=0

or

git rev-list --reflog --max-parents=0

But if it's required to find root commits including non-reachable ones, git fsck could be used:

git fsck --root --no-dangling

The --no-dangling option suppresses warning messages about dangling commits making it easier to process output in scripts.

emi
  • 2,786
  • 1
  • 16
  • 24
reddot
  • 764
  • 7
  • 15
1

To get first commit on the repository, run this command:

 git rev-list --oneline HEAD | tail -n 1
Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34