Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

githooks

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 abort.

But pre-commit hook has no arguments. I don't know how to detect --amend.

What should I do ?

like image 948
ton Avatar asked Oct 15 '13 17:10

ton


People also ask

How do pre-commit hooks work?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

How do you bypass a pre-commit hook?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.

Where do pre-commit hooks go?

All git hooks are stored in the . git/hooks/ directory under your project root. A pre-commit hook is just an executable file stored in this folder with the magic name pre-commit . Note that some code editors make the .


1 Answers

Following @Roger Dueck's answer, ended up doing:

#./.git/hooks/prepare-commit-msg

IS_AMEND=$(ps -ocommand= -p $PPID | grep -e '--amend');

if [ -n "$IS_AMEND" ]; then
  return;
fi
like image 127
kunicmarko20 Avatar answered Sep 20 '22 09:09

kunicmarko20