541

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??

mrutyunjay
  • 6,850
  • 7
  • 25
  • 35
  • 2
    Check this similar question http://stackoverflow.com/questions/6218199/git-commit-with-no-commit-message – Montaro Dec 14 '15 at 19:02

7 Answers7

666

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"
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
sarjit07
  • 7,511
  • 1
  • 17
  • 15
  • 36
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) 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](/review/low-quality-posts/26120160) – double-beep May 14 '20 at 06:35
  • what does the -m do? – Adam B Sep 17 '20 at 18:13
  • 7
    `-m` - Message for the commit. https://git-scm.com/docs/git-commit – Vizionz Sep 17 '20 at 19:47
  • 2
    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 Jan 12 '21 at 11:24
  • 2
    @double-beep The code was arguably self-documenting enough, but I added an explanatory sentence. – M. Justin Apr 04 '22 at 18:29
  • This doesn't answer the OP's question regarding the disadvantages/consequences and long-term effects of empty commits. – joeljpa May 08 '23 at 11:08
108

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
44

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

joeljpa
  • 317
  • 2
  • 13
Frido Emans
  • 5,120
  • 2
  • 27
  • 42
21

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.

yamafontes
  • 5,552
  • 1
  • 18
  • 18
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.

153957
  • 404
  • 6
  • 18
0

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 :)

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
grenix
  • 623
  • 7
  • 15
-3

What about this;

git commit --allow-empty-message -m ''
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
David Okwii
  • 7,352
  • 2
  • 36
  • 29