How to show first commit by 'git log'?

后端 未结 6 1875
野趣味
野趣味 2020-11-30 16:50

I have a project which has long history. I want to show the first commit on git.

How do I do this?

相关标签:
6条回答
  • 2020-11-30 17:29
    git log $(git log --pretty=format:%H|tail -1)
    
    0 讨论(0)
  • 2020-11-30 17:34

    Short answer

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

    (from tiho's comment. As Chris Johnsen notices, --max-parents was introduced after this answer was posted.)

    Explanation

    Technically, there may be more than one root commit. This happens when multiple previously independent histories are merged together. It is common when a project is integrated via a subtree merge.

    The git.git repository has six root commits in its history graph (one each for Linus’s initial commit, gitk, some initially separate tools, git-gui, gitweb, and git-p4). In this case, we know that e83c516 is the one we are probably interested in. It is both the earliest commit and a root commit.

    It is not so simple in the general case.

    Imagine that libfoo has been in development for a while and keeps its history in a Git repository (libfoo.git). Independently, the “bar” project has also been under development (in bar.git), but not for as long libfoo (the commit with the earliest date in libfoo.git has a date that precedes the commit with the earliest date in bar.git). At some point the developers of “bar” decide to incorporate libfoo into their project by using a subtree merge. Prior to this merge it might have been trivial to determine the “first” commit in bar.git (there was probably only one root commit). After the merge, however, there are multiple root commits and the earliest root commit actually comes from the history of libfoo, not “bar”.

    You can find all the root commits of the history DAG like this:

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

    For the record, if --max-parents weren't available, this does also work:

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

    If you have useful tags in place, then git name-rev might give you a quick overview of the history:

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

    Bonus

    Use this often? Hard to remember? Add a git alias for quick access

    git config --global alias.first "rev-list --max-parents=0 HEAD"
    

    Now you can simply do

    git first
    
    0 讨论(0)
  • 2020-11-30 17:44

    I found that:

    git log --reverse
    

    shows commits from start.

    0 讨论(0)
  • 2020-11-30 17:45

    Not the most beautiful way of doing it I guess:

    git log --pretty=oneline | wc -l
    

    This gives you a number then

    git log HEAD~<The number minus one>
    
    0 讨论(0)
  • 2020-11-30 17:51

    git log --format="%h" | tail -1 gives you the commit hash (ie 0dd89fb), which you can feed into other commands, by doing something like

    git diff `git log --format="%h" --after="1 day"| tail -1`..HEAD to view all the commits in the last day.

    0 讨论(0)
  • 2020-11-30 17:53

    You can just reverse your log and just head it for the first result.

    git log --pretty=oneline --reverse | head -1
    
    0 讨论(0)
提交回复
热议问题