Is there a special name (like HEAD, FETCH_HEAD) for the root commit on the current branch in a git repository?

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?

  • git-svn: what's the equivalent to `svn switch --relocate`?
  • git workflow in research environment
  • git: Is there a command line option for “Sort by date” for gitk?
  • How to get Vim to open .git\COMMIT_EDITMSG instead of .git\.git\COMMIT_EDITMSG?
  • use git cherry-pick now but merge changes later
  • To use <git push> on Visual Studio Code, but show “Could not read from remote repository.”
  • Why does “git log --tags=<pattern>” add “refs/tags” to the output?
  • Git: Possible to use same submodule working copy by multiple projects?
  • What is the simplest way to do branching and merging using TortoiseSVN?
  • Create a directory in Gitlab repo through web interface
  • How to change the “both added” and “both modified” colors during a Git merge in Git Bash
  • How to detach subdirectory in Git but keep all branches
  • 3 Solutions collect form web for “Is there a special name (like HEAD, FETCH_HEAD) for the root commit on the current branch in a git repository?”

    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.

    To find SHA-1 identifiers of all root commits (starting with any local branch) in a git repository, you can use the following command:

    $ git rev-list --parents --all |
      grep -v ' '
    

    You can then use git show to examine them.


    Explanation: git rev-list option --parents makes it print space separated list of all parents of the commit (in the form “commit parent…”). Therefore if a commit has any parents, the line from git rev-list invocation contains at least one space separating commit id from its parent id(s).

    The option --all means that git rev-list works as if all the refs in refs/ are listed on the command line as <commit>.

    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.

    Git Baby is a git and github fan, let's start git clone.