7

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?

Scott C Wilson
  • 16,711
  • 9
  • 54
  • 76
  • 1
    Was the branch squashed or rebased? If so, neither of those will show up in `--merged`. – Obsidian Age Jul 03 '19 at 22:41
  • No to being squashed/rebased. – Scott C Wilson Jul 03 '19 at 22:53
  • `git pull` on your main branch so that your merged commit appears in your branch's history. – meager Jul 03 '19 at 23:44
  • I have done that - but it's not sufficient. Still don't see the branch in `git branch --merged` – Scott C Wilson Jul 04 '19 at 00:14
  • I think what was happening was the commiters (different people from me) were taking my branch, adding modifications, and then merging. When this happens, git branch --merged will not show the original branch as merged. If someone wants to write this up I'll be glad to award points. – Scott C Wilson Jul 20 '19 at 15:50

3 Answers3

2

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!

Ross Hunter
  • 131
  • 4
  • Re: 1 - yes, I never merged it directly back in but I would have thought that when I merged in the changes to the upstream branch that I forked, it would be counted as merged in. I am even with the upstream branch, and the upstream branch merged in my change; doesn't that mean I have merged in my change? – Scott C Wilson Jul 04 '19 at 10:33
  • Normally I would think so, yes. Git identifies merged branches by checking if the tip commit of a given branch is accessible in the commit history of the commit you run the branch command on, so the next thing I would do is compare the commit histories of upstream/master and local/master by running git log. – Ross Hunter Jul 04 '19 at 23:43
0

I had similar problem, when feature branches were merged to develop and develop was merged to master. Such branches are not shown for

git branch --merged master, 
but shown for
git branch --merged develop

But it’s better to use -r flag as recommended in How can I delete all Git branches which have been merged?

Remote git clear: 

git branch -r --merged | grep -v '\*\|master\|develop\|release' | sed 's/origin\///' 
| xargs -n 1 git push --delete origin
Michael Freidgeim
  • 21,559
  • 15
  • 127
  • 153
0

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.

Dharman
  • 21,838
  • 18
  • 57
  • 107
ktsangop
  • 751
  • 2
  • 11
  • 24