636

I have pushed one commit to remote but now I realized that the commit message is not correct. I would like to change the commit message but AFAIK it is not possible. So i decided to create empty commit with correct message:

git commit --allow-empty

Are there any disadvantages/consequences of pushing empty commits? Is there any problem I might face in future because of this empty commit??

1

8 Answers 8

860

One use of an empty commit would be to force a build to occur in an environment where builds are triggered whenever new commits are pushed.

git commit --allow-empty -m "Trigger Build"
7
  • 41
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review Commented May 14, 2020 at 6:35
  • 8
    -m - Message for the commit. git-scm.com/docs/git-commit
    – Vizionz
    Commented Sep 17, 2020 at 19:47
  • 3
    Be aware that if your git index is not pointing to HEAD (i.e. you have files 'added'), those files will be commited. You may want to execute git reset first to clean the index. Note that git reset won't change the content of your working tree so you won't lose your working changes.
    – SimonC
    Commented Jan 12, 2021 at 11:24
  • 5
    @double-beep The code was arguably self-documenting enough, but I added an explanatory sentence.
    – M. Justin
    Commented Apr 4, 2022 at 18:29
  • 3
    This doesn't answer the OP's question regarding the disadvantages/consequences and long-term effects of empty commits.
    – joeljpa
    Commented May 8, 2023 at 11:08
114

You won't face any terrible consequence, just the history will look kind of confusing.

You could change the commit message by doing

git commit --amend
git push --force-with-lease # (as opposed to --force, it doesn't overwrite others' work)

BUT this will override the remote history with yours, meaning that if anybody pulled that repo in the meanwhile, this person is going to be very mad at you...

Just do it if you are the only person accessing the repo.

6
  • 10
    Yes thats why i preferred pushing empty-commit
    – mrutyunjay
    Commented Nov 22, 2013 at 6:49
  • 4
    @mrutyunjay if there's the chance other people pulled the repo, then yes, I think it's a nicer option. Commented Nov 22, 2013 at 6:50
  • 4
    You won't face any terrible consequence, just the history will look kind of confusing -- what a terrible consequence! ;-) Commented Aug 10, 2016 at 2:34
  • 8
    Prefer --force-with-lease to avoid losing collaborators' work.
    – Kenzo
    Commented Oct 23, 2018 at 8:30
  • @GabrielePetronella hilarious! lol yeah, yours can always break good friendships. Commented Dec 16, 2020 at 10:13
48

pushing commits, whether empty or not, causes eventual git hooks to be triggered. This can do either nothing or have world shattering consequences.

5
  • 24
    This is an underrated observation. Yes, empty commits may be superior to amending pushed commits, but they can't be assumed to be side-effect free if there might be hooks in place. Commented Apr 22, 2016 at 21:17
  • 1
    Alternatively if you're testing new web hooks... pushing empty commits seems like a decent enough idea Commented Oct 5, 2021 at 14:59
  • @fIwJlxSzApHEZIl Especially if done on a throwaway branch
    – M. Justin
    Commented Apr 4, 2022 at 18:34
  • Could you give an example of such "shattering consequences" please? I am curious :) Commented Jul 26, 2022 at 21:56
  • @alexis_thual those would entirely depend on your hooks. :)
    – crthompson
    Commented Oct 6, 2023 at 20:05
23

Is there any disadvantages/consequences of pushing empty commits?

Aside from the extreme confusion someone might get as to why there's a bunch of commits with no content in them on master, not really.

You can change the commit that you pushed to remote, but the sha1 of the commit (basically it's id number) will change permanently, which alters the source tree -- You'd then have to do a git push -f back to remote.

0
17

As long as you clearly reference the other commit from the empty commit it should be fine. Something like:

Commit message errata for [commit sha1]

[new commit message]

As others have pointed out, this is often preferable to force pushing a corrected commit.

6

If you want to trigger a CI build without extra commits, you can use:

git commit -m 'ci: empty commit' --allow-empty && git push && git reset --soft HEAD~ && git push -f

For convenience, you can make an alias for it (git ci):

git config --global alias.ci "!git commit -m 'ci: empty commit' --allow-empty && git push && git reset --soft HEAD~ && git push -f"

Then just use git ci, an empty commit will be pushed to the remote and then got removed.

2

As for confusing history I have a proposal for eventually making life of future archeologists easier.

git commit --allow-empty -m "message for commit DEADBEF was wrong. Correct Msg: ..."

where DEADBEF stands for the commit hash (SHA) of the commit with the wrong message.

Or if empty commit is not possible/allowed one could create a document

commitcomments/$SHA.txt

add/commit it and push it.

BTW: Cant resist to let on that git history is very often confusing anyway :)

-3

What about this;

git commit --allow-empty-message -m ''
2
  • While not really applicable to this question, I didn't know about --allow-empty-message (only --allow-empty). This is perfect for what I need, thanks.
    – Josh
    Commented Jan 11 at 12:50
  • 1
    Useful information, but not in the context of answering this particular question.
    – M. Justin
    Commented May 29 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.