2

Is there a way to protect a given git commit from being amended?

All I can think to do is to write a pre-commit hook to check the SHA against a list of 'non-amendable' SHAs. Does anyone know of a standard/better way?

Thanks.

user1675642
  • 747
  • 2
  • 5
  • 15

2 Answers2

3

You can tag commits using a private key so that if the commit is amended no one without the key can sign the new commit.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • Thanks for your answer. That's useful to know. In this case it's not so much that I'm worried about other people amending the commit. I'd like to protect it from being amended by me either. (Although it would be OK if it were ultimately possible to override the protection) – user1675642 Jan 21 '13 at 20:53
  • 1
    using the signing option `-S` for the original commit, you won't be able to amend unless you explicitly specify the option `-S` again. That sound like a good "safety" mechanism. – Gabriele Petronella Jan 21 '13 at 20:55
2

You can consider signing your commit with a GPG key.

In order to do so

  • Add your GPG key as a signing key

    git config --global user.signingkey YOUR-KEY
    
  • Commit using the -S option

    git commit -S -m "Your commit message"
    
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235