Is it possible to have git check the remote branch in the prepare-commit-msg
hook and see if the current commit is an amend, and if so, abort the commit?
That way it makes sure that you can not amend a commit that you already pushed, which would be handy.
Asked
Active
Viewed 593 times
4

FalcoGer
- 2,278
- 1
- 12
- 34
-
Either you're forbidding rewriting history altogether, or you don't. But an amended commit is just *a different commit*. On which basis do you intend to differentiate your "amends"? – Romain Valeri Jun 04 '19 at 12:13
-
when you commit using --amend, it doesn't change the commit. if you already pushed, you can't push the amended commit again unless you --force. i want to prevent amending commits before that happens, as then you have to do cleanup, or screw over everyone who already checked out the not amended commit. – FalcoGer Jun 04 '19 at 12:32
-
That's precisely the misunderstanding I was suspecting : I have to insist, **yes**, using `--amend` very much changes the commit. So much so that it's even arguably indistinguishable from any other commit in the tree after the amend. That's the reason for the needed `--force`. You're discarding the old commit and replacing it with the new, but git is not aware that the new one is the result of an amend. It's just *a different commit*. – Romain Valeri Jun 04 '19 at 12:54
-
Have you figured a way to do precisely that which you are asking, regardless of the counter-productive suggestions that you can't, or that you shouldn't ? – Mike Nakis Oct 06 '22 at 06:22
1 Answers
4
There is no way to prevent someone doing git commit --amend
on their own machine.
You can however prevent people from force pushing git push --force
and rewriting the repository history. This is done with a server side hook e.g. see Github's Blocking force pushes to a repository.

Karol Dowbecki
- 43,645
- 9
- 78
- 111
-
i thought one might be able to do some fancy shell scripting to figure out it's an amend. – FalcoGer Jun 04 '19 at 12:34
-
2You can ask people to use a [pre-commit hook](https://stackoverflow.com/a/41986190/1602555) but that's IMHO counter productive. It will be better to educate them that `push --force` is not allowed and rewriting history is a bad idea. – Karol Dowbecki Jun 04 '19 at 12:37