How to commit only a message to GIT?

Note: if you have staged changed, but want to create an empty commit with only a message, then you need (with Git 2.12, Q1 2017):

git commit --only --allow-empty -m "my message"

"git commit --allow-empty --only" (no pathspec) with dirty index ought to be an acceptable way to create a new commit that does not change any paths, but it was forbidden, perhaps because nobody needed it so far.

See commit beb635c (09 Dec 2016), and commit 319d835 (02 Dec 2016) by Andreas Krey (apk).
(Merged by Junio C Hamano -- gitster -- in commit 3aead1c, 19 Dec 2016)

commit: make --only --allow-empty work without paths

Signed-off-by: Andreas Krey
Reviewed-by: Jeff King

--only is implied when paths are present, and required them unless --amend.
But with --allow-empty, it should be allowed as well: it is the only way to create an empty commit in the presence of staged changes.


Yes. It is possible. I can never remember how to do this and always need to Google (so I am writting a Q/A in hope that this will help me to remember).

The command is:

git commit --allow-empty --only

The doc for --allow-empty reads:

Usually recording a commit that has the exact same tree as its sole parent commit is a mistake, and the command prevents you from making such a commit. This option bypasses the safety, and is primarily for use by foreign SCM interface scripts.

Doc makes sense but isn't easy to search for.


You might also consider use an annotated tag instead. You could then remove the tag later without having to rewrite history. To me this makes more sense than adding empty commits to your history.

git tag -a TODO-feature-foo -m "next I'm going to..."
git push origin :TODO-feature-foo

Once no longer needed:

git push origin :TODO-feature-foo

Tags:

Git