简体   繁体   中英

Git post-merge hook, how to get name of merged branch

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?

eg

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 at HEAD^2 (and possibly HEAD^3 , HEAD^4 in the case of an octopus merge) and test whether they are on particular branches.

To test if HEAD^2 is on the branch foo , you can test whether git merge-base HEAD^2 foo is the same as git rev-parse --verify HEAD^2 .

You can just use $GIT_REFLOG_ACTION in your hook. It contains text like merge some_branch_name, or pull origin some_branch_name or so on.

Well you can use git branch --contains <commit> to list all the branches; not sure that accomplishes exactly what you want as it will list any branches that contain that commit, but presuming you're merging in a new commit, it might do the trick...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM