Azure DevOps not deleting the secondary branch merged into master/Main

I’ve created a branch policy on my Main branch in Azure DeVOps Services, which requires a PR be made to commit anything. Then I created a new branch locally, made my changes there. Committed locally, then pushed that to Azure DevOps, which created the same branch on the remote. (I am using VS 2019, so it did that for me.) Next I created a PR, to take the changes in the secondary branch and apply them to Main. That has worked fine.

However, I still have that secondary branch, both on the remote and locally. I thought Azure DevOps would have deleted the secondary branch, but it didn’t. Two questions: why didn’t the PR delete the secondary branch? And what is the proper way for me to get rid of the secondary branch, both on the remote and locally?

When you create a pull request, there’s a checkbox option to delete the branch after the pull request completes. If that box is unchecked, Azure DevOps will not perform the delete automatically.

Thank you, must have missed it.

Sounds like you’re new to Git in addition to Azure DevOps. You will probably want to go through some Git tutorials.

Since Git is a distributed VCS, how to manage it on your local system is usually personal preference within the guidelines of your organization. From the command line, git branch -d <branchName> will remove it locally.

I’ve done that before, but somehow that got me pretty messed up. As though I had tried to do that to Main, which I no longer can do. Or is it the case that issuing the command git branch -d <newName> is not a part of a commit to Main?

For example, I know that I should not delete a branch that I’m on. So, I do a git checkout main to go to the main branch, then before when I did this, I issued the git branch -d <branchName> command, and then’s when I got messed up.

You need to specify the branch you are deleting

Sorry, I did specify the name of the branch I wanted to delete. I just left it off of the text I wrote here, but I’ve gone back and added it in

The delete command does not commit anything. It’s effectively removing a special tag (the branch name) from your repository.

I’ve just issued the git command to delete the local branch. However, I’ve got a warning, probably the same warning I got previously when I tried doing a PR from a different branch to main. Here’s the full text of what I issued and the warning from git:

warning: deleting branch 'remove-commented-code' that has been merged to
         'refs/remotes/origin/remove-commented-code', but not yet merged to HEAD.
Deleted branch remove-commented-code (was b5e2559).```

I wasn’t aware of the fact that apparently I had to merge the secondary branch to HEAD. Even though it is too late to do that now, how should I have gone about doing that?

Your local repository is saying your branch remove commented-code was merged (by issue the push command earlier) to origin/remove-commented-code but is not merged to HEAD which is likely the latest place your repository is pointing to in main . If the pull request was completed and merged remotely, then you can issue a pull command to have your local repository retrieve those remote changes.

WOW, that’s weird, because that is exactly what I did. Here’s the full text of the commands

Already up to date.
C:\NewProj\ACDC [main ≡]> git branch
* main
  remove-commented-code
  sendmail-work
C:\NewProj\ACDC [main ≡]> git checkout remove-commented-code
Switched to branch 'remove-commented-code'
Your branch is up to date with 'origin/remove-commented-code'.
C:\NewProj\ACDC [remove-commented-code ≡]> git pull
Already up to date.
C:\NewProj\ACDC [remove-commented-code ≡]> git main
git: 'main' is not a git command. See 'git --help'.

The most similar command is
        mailinfo
C:\NewProj\ACDC [remove-commented-code ≡]> git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
C:\NewProj\ACDC [main ≡]> git branch
* main
  remove-commented-code
  sendmail-work
C:\NewProj\ACDC [main ≡]> git branch -d remove-commented-code
warning: deleting branch 'remove-commented-code' that has been merged to
         'refs/remotes/origin/remove-commented-code', but not yet merged to HEAD.
Deleted branch remove-commented-code (was b5e2559).```

Because your changes still exist (in the remote branch, as far as your local repository thinks), it is only warning you that it isn’t merged to HEAD. The last line of the message says it deleted the branch.

If the branch was never pushed, and therefore the only record of it exists locally, it would instead issue an error, refuse to delete your local branch, and provide instructions on how to force the deletion.

Is the problem that I was on the wrong branch, when I issued the git pull command? Should I have been on remove-commented-code?

No. Is the pull request complete in Azure DevOps or is it still waiting for some branch policies to finish, like an approval?

BOOM!! That was it. When I was in the Pull Request second of Azure DevOps, I saw two buttons, one labeled “Approve” and the other labeled, “Complete”. I naively thought that approving the PR would complete the PR. I was wrong about that. It was still waiting for me to complete the PR. I’ve just done so. Now the remove-commented-code branch is gone off of the remote. And I’ve already deleted it locally.

Thank you very much, !!! :grin:

Anyone with permission to complete a pull request should be able to set the Auto-complete setting so that the merge completes as soon as all criteria is met.

I’ll look into changing that, for this project. Thank you.