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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With