1233023
1233023

Reputation: 321

Getting the merge branch name of a merge commit

Git is such a well written piece of software, that you can spend a long time using it without really understanding whats going on.

I'm trying to get the name of the branch which was merged with the master branch most recently, and I don't seem to be getting anywhere with git log, git show etc.

Github has the branch name history when I look at the commits to master, but I'm wondering if this is something that github is keeping track of additionally and not something I can access by git. Any help would be much appreciated!

Upvotes: 3

Views: 4781

Answers (3)

lam vu Nguyen
lam vu Nguyen

Reputation: 641

if you create pull requests (black circle) and merge branch in github, you can find information of a merge commit by clicking in 22 Closed (red circle)

github

Upvotes: 0

Eugene Kulabuhov
Eugene Kulabuhov

Reputation: 2499

As @torek mentioned in the comment, you can use a message within the merge commit itself.

#!/bin/bash
commit_subject=$(git log -1 --pretty=format:%s)
echo Commit subject: $commit_subject

regex='Merge pull request #[0-9]+ from .+/(.+)$'
[[ $commit_subject =~ $regex ]]
branch_name=${BASH_REMATCH[1]}
echo Merged branch: $branch_name

Example output:

Commit subject: Merge pull request #2574 from RepoName/BranchName
Merged branch: BranchName

Upvotes: 0

VonC
VonC

Reputation: 1326892

As commented, this information is not recorded.

You might consider a post-merge hook, like this one, which would:

  • get the branch that was just merged (through git reflog)
  • use git notes add in order to add the the current merge commit the information you want.
    That way, no need to modify the description/merge commit message.

That is a local hook though, that would need to be installed on every cloned repository. If you are alone working on the repository, that would work.

Upvotes: 2

Related Questions