Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git branch --merged not show all merged branches?

Tags:

git

I created a feature branch, then created a PR for it. My PR was accepted and merged. The PR says, "the branch can be safely deleted." But when I do git branch --merged on the main branch, I don't see the merged branch. What I am doing wrong?

like image 889
Scott C Wilson Avatar asked Jul 03 '19 22:07

Scott C Wilson


People also ask

How can you see all branches with merged work?

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.

Why the branch is not fully merged?

There are times when you get an “not fully merged” error for a git branch, usually when trying to delete a local branch from your machine. It's a safe bet that something in your local branch has not actually made it to the remote repository and we should do some investigating.

Do branches disappear after merge?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.

How can you tell if two branches are merged?

Suppose you are on branch master and you want to check if the git merge origin/devel will work. Then just do: git merge-tree `git merge-base origin/devel master` master origin/devel . But this one line is way more powerful than it looks, because you can also do this on a branch that is not checked out.


2 Answers

I have been struggling with this also. We are using gitlab's MR flow, which is probably the most simple merge based git flow.

What I have noticed is that we select to Squash Commits when a MR is merged. As a consequence this affects git history and prevents the --merged flag from working as expected.

like image 140
ktsangop Avatar answered Sep 19 '22 13:09

ktsangop


This is a difficult question to answer without knowing the exact workflow.

I assume since you 'created a PR' you forked/cloned a non-local repo to start with. Then you created a new branch in your local repository, made changes to add a feature, and committed those changes onto your local feature branch.

Beyond that it's a bit murkier. Here are a few steps you might take:

  1. You say you submitted a PR, but you don't say that you ever merged the feature branch with your local master branch. That suggests you may be following a workflow like this one. If that's the case, and you're running git branch --merged in your local repository, the reason you don't see your feature branch listed is that you never merged your feature branch into master in your local repository. This, IMO, is the most likely scenario. Try running git pull <name of your remote--probably origin> master from your local master branch, then trying running git branch --merged again.

  2. Fast-forwarding could cause some confusion, though it wouldn't create the issue you're describing on its own.

  3. You can always run git log on a given branch to see its full commit history. You could examine the commit history of your master and compare it to the commit history of origin/master to maybe find the discrepancy.

Hope this helps!

like image 34
Ross Hunter Avatar answered Sep 21 '22 13:09

Ross Hunter