Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unmerged Git branches?

I have a Git repository with many branches, some of them already merged and some not. Since the number of branches is quite large, how can I determine which branches have not yet been merged? I would like to avoid having to do an "octopus" merge and re-merging branches that have already been merged.

like image 268
fluca1978 Avatar asked Sep 05 '12 06:09

fluca1978


People also ask

How do you check the list of those branches you haven't yet merged in to the main branch of the repository?

You can use the git merge-base command to find the latest common commit between the two branches. If that commit is the same as your branch head, then the branch has been completely merged.

How can you tell if two branches are merged?

Find the merge base, and then check if git diff --name-only $merge_base branchA and git diff --name-only $merge_base branchB have anything in common. Otherwise, you'll need a work tree to try the merge in. You could easily create a second one - either clone the repository, or to save space, just create a work tree.

Do merged branches get deleted?

The more the branches and master diverge away from each other the farther away their “common ancestor” commit becomes. 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.


2 Answers

Try this:

git branch --merged master 

It does what it says on the tin (lists branches which have been merged into master). You can also pull up the inverse with:

git branch --no-merged master 

If you don't specify master, e.g...

git branch --merged 

then it will show you branches which have been merged into the current HEAD (so if you're on master, it's equivalent to the first command; if you're on foo, it's equivalent to git branch --merged foo).

You can also compare upstream branches by specifying the -r flag and a ref to check against, which can be local or remote:

git branch -r --no-merged origin/master 
like image 126
Amber Avatar answered Oct 19 '22 07:10

Amber


You can also use the -r parameter to show remote branches that were not merged into master:

git branch -r --merged master  git branch -r --no-merged                            
like image 45
NemoXP Avatar answered Oct 19 '22 06:10

NemoXP