Hacker News new | ask | show | jobs
by mywittyname 1349 days ago
What's the purpose of this? I can think of ways to use/abuse it, but there has to be a specific reason that it was added as a feature to git.
8 comments

The most common reason I see it is to create the initial commit of a repo. This is useful both so you have something to push to a remote, PR against, and because several git commands (most notably rebase variants) need weird switches (eg --root) if you _don't_ have an empty initial commit to refer to instead.
git commit --allow-empty -m "trigger CI job"
Nice. I used to just add a BOM at the beginning of a random line to accomplish that.
I think this post is a good reason not to do that ;)
I use it extensively for entire PR based development; that way all the code that ended up in the repo has had multiple eyes (and an opportunity to comment) upon it

    git commit --allow-empty -m "initial"
    git push -u origin HEAD
    git checkout -b first-pr
    # type-y type-y
I've found it useful when interactively rebasing a series of commits to make a nice PR. The code moved to a different commit, but I didn't want to lose the message.
I see `--allow-empty` in git tutorials all the time, to demonstrate the concept of a commit. I'm not sure when you'd reach for it in a real repo though
The most obvious reason not involving automated systems is to create a repo’s initial ref. This can be useful on teams who are super strict about review process and git history.

Once you account for automated systems, many other reasons can arise. Not just the “trigger CI” case mentioned in another comment, triggering builds or processes based on remote content the code accesses, or generating something either random or seeded by the commit hash/timestamp/message/etc.

It can even be a Homer Simpson-style drinking bird button press, so finished software doesn’t get mistaken for abandoned.

Probably whole worlds of things I haven’t imagined because I don’t use git hooks or submodules.

For those of us who start projects often, but never finish them:

    git commit --allow-empty -m "Initial commit."
I use it to trigger CI/CD. And for example, when upgrading Heroku's dyno stack, when is applied on next deployment.

    heroku stack:set heroku-22
    git commit -m 'upgrade to heroku-22 stack' --allow-empty
    git push heroku master
Maybe so you can make an empty initial commit and push it to a remote like Github as a placeholder? Or a lazy way to trigger a CI job when you're gonna squash and cleanup later.

I guess conceptually you could use it to represent "I started from nothing."