0

I want to know merged branches from merge commit.

So, I decide to automatically write a merge message which contains merging branches when the merge happens by using a commit hook.

However, this has a problem. I couldn't know which branches are merged when I just see a commit object.

Is there a good way to know which branches are merged while just seeing a commit object?

Melebius
  • 6,183
  • 4
  • 39
  • 52
Ui-Gyun Jeong
  • 143
  • 1
  • 4
  • 14
  • 2
    Bear in mind that branches are literally just references to specific commits. So you can ask questions like "do this merge commit and this branch share a common ancestor?", but "which branches were merged?" isn't necessarily meaningful from Git's point of view. – Oliver Charlesworth Nov 27 '17 at 12:46
  • I am not sure what you are trying to get (objects or branch name?). You can identify the type of the commit (merge) and determine the common ancestor. Now `git show ` shows `Merge: commit commit` which are the tip of the 2 branches merged. I couldn't figure out how to obtain it in a better way (yet). From there you can use `git merge-base ` in order to obtain the common ancestor, i.e. the object where the branch was created. – Fabien Bouleau Nov 27 '17 at 13:03

1 Answers1

0

You can get the parent commits of a merge commit with the following command:

git rev-list --parents -n 1 <commit>

As stated in my earlier comment, you cannot get the branch names, only the commits. Then from the two commits, you can get the fork point with:

git merge-base <commit> <commit>

Bonus: the order of the commits matter. The left commit is the branch where you executed the merge and the right commit is the one you merged. For instance if you are in your master branch and do git merge --no-ff develop, the left commit will be master and the right one develop.

Fabien Bouleau
  • 464
  • 3
  • 11