3

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!

1233023
  • 311
  • 7
  • 19
  • 4
    Except for the default merge message (`merge branch B` or `merge branch B into T`), Git does not save this information anywhere. Branch names are movable and delete-able, so if the name has been moved or deleted, there's nothing to find. – torek Sep 04 '20 at 06:01
  • 1
    Branches are just names. Merges involve only commits, even if (and yes the metaphor can be confusing) we often use branch names to refer to them. – Romain Valeri Sep 04 '20 at 06:02
  • I see, so the only way I can distinguish classes of merges, is by adding some identifier in the description? Because I can get that str. from git log for instance. – 1233023 Sep 04 '20 at 06:03
  • Why do you need to know the branch name that was originally used? – Lasse V. Karlsen Sep 04 '20 at 06:37
  • I want different deploy scripts to run depending on what kind of branch merge occurred. – 1233023 Sep 04 '20 at 07:02

2 Answers2

2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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
Eugene Kulabuhov
  • 2,349
  • 1
  • 26
  • 25