2

I am planning to write a pre commit hook. It should check whether the master branch have any versions after branching out of a file.

Example. A file abc.txt has branched out from master, on 1st Jan. Say, the branch name is BR1.X. On BR1.x branch, the file was worked till 10th Jan. On MAster branch say on 5th Jan some changes were happened. So pre commit should just give a message saying after branching out there were changes happened on master. Please help how to get these details from git?

1 Answers1

2
  1. Find the common ancestor of the current branch and the master branch:

    ANCESTOR=`git merge-base HEAD master`
    
  2. Find the files that are staged for commit:

    STAGED=`git diff --cached --name-only --diff-filter=ACM`
    
  3. Check if these files have been modified on master:

    git diff --name-only $ANCESTOR master $STAGED
    

And do whatever you need to do with this information

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111