brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git rev-list with max-parents=0 finds the very first commit

Git
~/tips/tip-git-rev-list-first-commit

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

Prints the root commit of a repository, without scrolling to the end of the log.

Scrolling to the bottom of git log on an old project takes ages. The first commit is simply the one without a parent, and git can filter on that.

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

--max-parents=0 keeps only commits that have no parent at all. In most repositories that’s exactly one: the initial commit. Pass the hash to git show to see what the project looked like on day one:

git show --stat $(git rev-list --max-parents=0 HEAD)

I like this for an old project you just joined, when you want to know when it started and who made it. It’s also how you find the base for a diff of everything ever, though git diff against the empty tree is another way to do that.

The same option works with git log, so git log --max-parents=0 --oneline gives a readable line instead of a bare hash.

Don’t assume there’s only one. A repository that merged in another project with its own history, or a branch made with git switch --orphan, has more than one root, and you’ll get several lines back. Add --all if you want roots from every branch, not only from the one you’re on. And in a shallow clone the oldest commit you have isn’t the real first commit at all, because git simply never downloaded the older ones.

Further reading: git-rev-list documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link