1

When anybody makes a change to a specific file in my Git repo, I'd like to display a message to the user when they commit or push via Git CLI.

Scenario: User alters crazycalculations.py, I'd like to say, "Hey, I notice you altered crazycalculations.py. Did you have John in Accounting review your change?". Just a friendly reminder to the end user that their messing with a file that is not to be messed with without supervision.

Any suggestions on how I can accomplish this?

harperville
  • 6,921
  • 8
  • 28
  • 36

1 Answers1

4

Yes, you can do this with git hooks. If you navigate to .git/hooks you'll see samples for shell code that can be run on specific git events. Check the .git/hooks/pre-commit.sample will have some sample code you can use.

For example, at the top of .git/hooks/pre-commit:

if git diff HEAD --name-only | grep -q "crazycalculations.py"; then
   echo "Are you sure you want to edit crazycalculations.py?"
   sleep 3
fi

enter image description here

Documentation:

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • Thanks for the example. This is a useful, followup question - adding hooks to the repo: http://stackoverflow.com/questions/3462955/putting-git-hooks-into-repository – harperville May 12 '16 at 19:55