tl;dr: Committing something, pushing that, amending that commit and trying to push again without using --force
fails. How to set up Git so that it tells, when I try changing 'published commits'?
I am using Git for quite some time now and it still is one of the best things which ever happened to me (software-wise): it totally supports the way I do stuff out of the box. Just yesterday I did something stupid: I committed some stuff, pushed it to my remote, left the work for some minutes and when I came back, I noticed that I changed one of the files after I had added it, so that the commit was not complete. As usual, I used git commit --amend
to fix this minor problem.
I worked on for several hours and decided that it was time for another git push
. To my dismay, git refused to that and reading the message, scrolling back some thousand lines I saw what I did and knew what was going on. The remote basically is nothing more than a private off-site backup and nobody else uses it to push or pull. So I did not run into trouble with anybody rewriting history using git push --force
. Since I would not like this to happen in a multi-user environment (either clean up my history or force-push) I thought about how to prevent this in future: Git could just issue a clearly visible warning, maybe even requiring me to type something. I'd notice and probably decide against amending.
So, the question is: how can I configure Git to warn me about amending commits which have already been pushed? Or more generally: how can I configure Git to warn me about changing published history?
Edit #1 (~6 h later): Unfortunately, I do not feel confident enough to try to implement this on my own, but maybe it helps somebody to help me :) I thought that since --amend
affects the latest commit only, it would suffice to have a pre-commit hook that checks the hash of the commit I am trying to amend and the current/latest commit pushed to the remote. For example currently I get b45025a...
as the latest commit to master on my local machine, I pushed that and cat .git/refs/remotes/<remote-name>/master
gives b45025a...
as well. So, would it work to check if the corresponding remote branch is at the same commit as the one I am trying to amend right now?
Before writing this I asked Google and also briefly looked over questions I found here, but nothing seems to fit (selection):