日本語

Git: Create an Empty Commit with No Changes Using –allow-empty

​​Git​command line

If you run git commit without changing any files, it prints "nothing to commit" and does not create a commit. Adding --allow-empty creates an empty commit with no changes. This is useful when you want to push without touching any files, just to re-run CI (such as GitHub Actions), or when you want the first commit of a new repository to be empty.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). The push destination was a bare remote repository I created in the same test environment. I did not test pushing to services such as GitHub, or whether CI actually runs.

The repository before the operation

I start with a repository called neko-app that has only one commit. The remote origin is the bare repository neko-app.git, which I created in the adjacent folder. The commit is already pushed there, and the working tree has no changes.

console
$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

$ git log
commit 7e817a63371d430dfe4dbdc35561d1ed25cafea8
Author: Neko <neko@example.com>
Date:   Thu Nov 12 09:50:00 2026 +0900

    トップページを作る

With no changes, git commit stops

In this state, I ran git commit as usual. It printed the same output as git status, and no commit was created. The exit code was 1.

console
$ git commit -m "CI を動かし直す"; echo "終了コード: $?"
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
終了コード: 1

I added echo "終了コード: $?" to print the exit code of the previous command. You don't need it normally.

Creating an empty commit with –allow-empty

When I added --allow-empty to the same command, the commit was created.

console
$ git commit --allow-empty -m "CI を動かし直す"; echo "終了コード: $?"
[main 37d2c43] CI を動かし直す
終了コード: 0

For a normal commit, the line after [main …] shows the number of changed files and lines, such as "1 file changed, 1 insertion(+)". Here, that line is missing.

The --allow-empty description in the official Git 2.43.0 reference says that creating a commit whose tree (the full set of file contents) is identical to its parent's is usually a mistake, so git commit refuses it by default. --allow-empty turns off that check.

History after the operation

git log shows the new commit added on top of the existing one.

console
$ git log
commit 37d2c43b024be95c91ffbbe8f170f01a2f3818ee
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:00:00 2026 +0900

    CI を動かし直す

commit 7e817a63371d430dfe4dbdc35561d1ed25cafea8
Author: Neko <neko@example.com>
Date:   Thu Nov 12 09:50:00 2026 +0900

    トップページを作る

git show --stat, which lists changed files, shows no file lines either.

console
$ git show --stat HEAD
commit 37d2c43b024be95c91ffbbe8f170f01a2f3818ee
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:00:00 2026 +0900

    CI を動かし直す

When I listed the tree hash (%T) each commit points to, both commits had the same value. The output of git diff against the previous commit was also 0 bytes.

console
$ git log --format="%h %T %s"
37d2c43 308be59e17f5ac89f324b945d438a28db2dfca7f CI を動かし直す
7e817a6 308be59e17f5ac89f324b945d438a28db2dfca7f トップページを作る

$ git diff HEAD~1 HEAD | wc -c
0

Empty commits can be pushed normally

An empty commit can be pushed just like any other commit. I replaced the path in the output with /path/to.

console
$ git push origin main
To /path/to/neko-app.git
   7e817a6..37d2c43  main -> main

$ git --git-dir=../neko-app.git log --oneline
37d2c43 CI を動かし直す
7e817a6 トップページを作る

The empty commit also reached the remote repository. The GitHub Docs page on events that trigger workflows says that in GitHub Actions, a workflow with on: push runs when commits or tags are pushed. The same page also describes how to filter by pushed branch or tag, or by changed files. If you want to re-run CI without changing files, create an empty commit and push it like this. However, a workflow with such filters only runs when its conditions match.

With staged changes, it becomes a normal commit

--allow-empty only allows a commit with no changes to be created. It does not turn a commit into an empty one. When I added it with staged changes, the commit included those changes and became a normal commit.

console
$ printf "<p>今日は晴れ。</p>\n" >> index.html && git add index.html

$ git commit --allow-empty -m "日記を1件書く"
[main 3e17e19] 日記を1件書く
 1 file changed, 1 insertion(+)

If you want to make an empty commit, check with git status first that you have no staged changes.

Emptying the message needs a separate option

--allow-empty permits an empty commit, but not an empty message. With -m "", the commit was aborted because the message was empty. To allow an empty message too, add --allow-empty-message separately (--allow-empty-message description in the official Git 2.43.0 reference).

console
$ git commit --allow-empty -m ""; echo "終了コード: $?"
Aborting commit due to empty commit message.
終了コード: 1

$ git commit --allow-empty --allow-empty-message -m ""; echo "終了コード: $?"
[main e48b4e8] 
終了コード: 0

$ git log --oneline
e48b4e8 
3e17e19 日記を1件書く
37d2c43 CI を動かし直す
7e817a6 トップページを作る

A commit with no message doesn't tell you what it is in git log --oneline. In my test, I removed this commit with git reset --hard HEAD~1.

Making the first commit of a new repository empty

You can also use --allow-empty to create the first commit in a repository right after git init, when it has no files. I left neko-app and created a new repository called neko-blog.

console
$ cd ..

$ git init -q neko-blog

$ cd neko-blog

$ git commit --allow-empty -m "最初のコミット"
[main (root-commit) 95c84af] 最初のコミット

$ git log --stat
commit 95c84afb445e62c5bd7e49001d929dcd8b408064
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:35:00 2026 +0900

    最初のコミット

When I displayed the commit contents with git cat-file -p, there was no parent line. The (root-commit) in the output indicates a commit with no parent.

console
$ git cat-file -p HEAD
tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
author Neko <neko@example.com> 1794447300 +0900
committer Neko <neko@example.com> 1794447300 +0900

最初のコミット

After this, you can add files and commit as usual.

console
$ printf "# ねこブログ\n" > README.md && git add README.md && git commit -q -m "README を書く"

$ git log --oneline
f4712e4 README を書く
95c84af 最初のコミット

Cherry-picking an empty commit stops

If you use git cherry-pick to bring an empty commit onto another branch, it stops by default. I went back to neko-app, created a topic branch with only the first commit, and cherry-picked the empty commit from main (37d2c43).

console
$ cd ../neko-app

$ git switch -q -c topic HEAD~2

$ git cherry-pick 37d2c43; echo "終了コード: $?"
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
On branch topic
You are currently cherry-picking commit 37d2c43.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

nothing to commit, working tree clean
終了コード: 1

The message mentions "conflict resolution," but there is no conflict. The commit was already empty, so the result of picking it is also empty, and cherry-pick stopped partway. The output suggests either running git commit --allow-empty to commit it, or running git cherry-pick --skip to skip it.

In my test, I first aborted with git cherry-pick --abort, then retried with --allow-empty on git cherry-pick. This time it went through without stopping.

console
$ git cherry-pick --abort

$ git cherry-pick --allow-empty 37d2c43; echo "終了コード: $?"
[topic 687754b] CI を動かし直す
 Date: Thu Nov 12 10:00:00 2026 +0900
終了コード: 0

$ git log --oneline
687754b CI を動かし直す
7e817a6 トップページを作る

The --allow-empty description for git cherry-pick in the official Git 2.43.0 reference also says that cherry-picking an empty commit fails by default, and that this option keeps it as an empty commit. However, only commits that were already empty are kept. A commit that becomes empty because the same change already exists in the target branch is not kept by this option. The documentation says --keep-redundant-commits is needed for that case.

​Posted by 86