Search code examples
gitgit-commitgit-push

Is there a way to display a message to an end-user when they commit a file in Git?


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?


Solution

  • 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