I’m trying to create post-merge hook script which runs only when merging from specific branch. How can I determine name of the branch changes came from for specific commit?
e.g.
if $from_specific_branch == 1 then
git diff --name-status HEAD@{1} HEAD "some_folder" |
while read st file; do
#skip deleted
if [ "$st" == 'D' ]; then continue; fi
# .. do something with those files
end
Firstly, it’s worth noting that you might be merging any commit into the current commit – it doesn’t have to be on any branch, in fact.
When you create a merge, I believe that the first parent (
HEAD^1) is always the branch you were on when merging. So, you can look atHEAD^2(and possiblyHEAD^3,HEAD^4in the case of an octopus merge) and test whether they are on particular branches.To test if
HEAD^2is on the branchfoo, you can test whethergit merge-base HEAD^2 foois the same asgit rev-parse --verify HEAD^2.