Fix incorrect “branch hasn’t been merged” warning when deleting identical branches
-
Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA. As a benefit of being a GitLab Community Contributor, you receive complimentary access to GitLab Duo.
What does this MR do and why?
Fixes: #419332 (closed)
GitLab incorrectly displayed a “branch hasn’t been merged” warning when trying to delete a branch that is identical to the default branch (same commit SHA).
Previously, even if two branches had the same commit history GitLab still showed the warning, making it confusing for users. This MR updates the logic to correctly detect and mark identical branches as merged ensuring that users can delete them without unnecessary warnings.
Screenshot
| Before | After |
|---|---|
![]() |
![]() |
How to set up and validate locally
- Visit a project (for example, http://gdk.test:3000/flightjs/Flight)
- Create a branch "test-branch" from "master" branch
- Try to delete the branch via UI
- The warning message doesn't mention that the branch hasn't been merged into master.
| header | header |
|---|---|
![]() |
![]() |
Previous reproduction steps
To reproduce the issue, follow these steps:
1. Clone the repository and start GitLab locally
git clone http://127.0.0.1:3000/flightjs/Flight.git
cd Flight
cd ~/gdk
gdk start
2. Open the Rails console
cd ~/gdk/gitlab
bundle exec rails c
3. Fetch the project and repository and list available branches and check if a branch is considered merged
project = Project.find_by_full_path("flightjs/Flight")
repo = project.repository
repo.merged_branch_names(["test-branch1"], include_identical: true)
Expected (Correct) Behavior:
If test-branch1 is identical to the default branch it should be detected as merged and appear in the list.
4. Expire the cache and re-check
repo.expire_branches_cache
repo.merged_branch_names(["test-branch1"], include_identical: true)
5. After applying the fix repeat the steps above and check if the issue is resolved:
repo.merged_branch_names(["test-branch1"], include_identical: true)
One of the problems we faced was that the branch merge status was cached causing inconsistent results.
Screenshot & Screen recording
Fixed the delete branch message in GitLab so it warns only when a branch is truly not merged. Before it shows the warning even for branches that were identical to the default branch (master/main). Below updated the logic to correctly detect merged branches. To test I created some branches (test-merged-1 & test-merged-2) that were updated to master and some that weren’t (test-unmerged-1). Now GitLab only warns for unmerged branches and allows deleting identical ones without confusion.







