How to detect commit --amend by pre-commit hook ?

后端 未结 5 1297
陌清茗
陌清茗 2021-02-19 07:09

When I do commit --amend, it is unsafe commit if the commit already has been pushed to remote repository.

I want to detect unsafe commit --amend by pre-commit hook and a

5条回答
  •  旧巷少年郎
    2021-02-19 07:25

    A quick way to detect a "pure" amend in the pre-commit hook:

    if git diff --cached --quiet ; then
      echo "This is a pure amend"
    else
      echo "This is a commit with changes"
    fi
    

    By "pure" I mean you're only rewriting the commit message and not any of the changes in the commit. If there are any changes in your index when you call git commit --amend, you're rewriting more than the commit message and this will behave as if you're doing a conventional git commit.

提交回复
热议问题