Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of branches that were merged into another branch

Tags:

git

branch

I'm having some trouble doing this in a non convoluted way... currently I can figure out which branches were merged into a branch by outputting branches --merged from the target branch and from master, writing the output from those commands to files, and then diffing those two files. Does anyone have a better way to get this info?

For example, say branches branch1, branch2, and branch3 are all cut from master and dev work is done on them. Then, branch1 and branch2 are merged into integrationBranch. The command should be able to tell me that branch1 and branch2 were merged into integrationBranch.

EDIT:

When I run git branch --merged, the branch that's merged in first is always dropped from the output.

Example:

git checkout inegraionBranch
git merge origin/branch1
git merge origin/branch2

git branch merged
*integrationBranch
branch2
like image 696
user797963 Avatar asked Apr 25 '14 14:04

user797963


People also ask

How do you list the branches that have been merged?

With git branch --merged <commit> , your local list of branches will be filtered by all the branches who have been merged into a given branch or commit. Similar to above, you could type git branch --no-merged <commit> and only the branches not merged into the named commit would be listed.

What happens to branches after merging?

When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.

Which command is used to get the list of all the available branches?

To view the current branch you are working on you can simply use the git status command. This command shows a lot of useful information about your current changes including what branch you are on: $ git status On branch main Your branch is up to date with 'origin/main'.


1 Answers

In the case of your example you'll want to use git branch -a --merged integrationBranch

The two branches origin/branch1 and origin/branch2 are remote branches and aren't listed with git branch by default so neither is showing up in your output unless you use a -a switch.

I'm guessing the reason you are seeing branch2 is that you probably have a local branch named branch2 which is merged in (ie, the problem is not that the command is dropping the first branch)

EDIT:

To get all of the branches merged into integrationBranch but not into master you could do:

git branch --list -a --merged integrationBranch | sed 's/^..//;s/ .*//' | xargs git branch --list -a --no-merged master
like image 141
jkyako Avatar answered Oct 12 '22 22:10

jkyako