1

I have a Gitlab repository with stale branches that I have not noticed before. I think they was created by Gitlab when one of the users made a change to the repository with the build in file editor. They have auto generated names looking like this 0a9cc10e66e4e7e48bf7a7fc302e5dc25dd21416.

The issue is, I don't know how to remove this branches. Gitlab UI return code 400 when I press on the "Delete branch" button. And git cli failing as well.

git branch --all

* master
  remotes/origin/0a9cc10e66e4e7e48bf7a7fc302e5dc25dd21416
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

git push origin --delete 0a9cc10e66e4e7e48bf7a7fc302e5dc25dd21416

remote: GitLab: You cannot create a branch with a 40-character hexadecimal branch name.

Is there any way to accomplish this task?

Otiel
  • 18,404
  • 16
  • 78
  • 126
Ivan Stasiuk
  • 292
  • 6
  • 12
  • 2
    Interesting. It looks like GitLab decided to forbid such branch names but failed to account for the deletion pushes. :-) – torek May 12 '21 at 00:52
  • Please improve the title of the post such that it summarizes the issue you're describing. The fact that it is a stale branch is irrelevant. – Błażej Michalik Jul 22 '21 at 14:00
  • @Otiel this is indeed solved the issue. Please add an answer, I will accept it. – Ivan Stasiuk Mar 12 '22 at 13:28

2 Answers2

1

The reason for the error response is an active push rule. GitLab offers custom hooks for git push events. One such hook is the following security check, activated by default.

By default, GitLab restricts certain formats of branch names for security purposes. 40-character hexadecimal names, similar to Git commit hashes, are prohibited.

https://docs.gitlab.com/ee/push_rules/push_rules.html#default-restricted-branch-names

Suggested Solution: Remove/Replace existing push rules.

This is a premium feature however.

All Projects: Open Admin Area > Push Rules

One Project only: Open Project > Settings > Repository > Tab »Push rules«

Alternative: Use the API to delete the branch.

curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://<your-gitlab-instance>.com/api/v4/projects/<project-id>/repository/branches/<branch-name>"
pixelbrackets
  • 1,958
  • 19
  • 28
1

This was a GitLab bug. They've added a check in GitLab 12.10 to prevent branches with 40 hexadecimal characters to be created:

See the documentation:

Default restricted branch names

Introduced in GitLab 12.10.

By default, GitLab restricts certain formats of branch names for security purposes. 40-character hexadecimal names, similar to Git commit hashes, are prohibited.

But they didn't allow then to delete such branches.

This has been fixed in v14.6, and deleting those legacy branches is now possible again: see the relevant issue and merge request.

Otiel
  • 18,404
  • 16
  • 78
  • 126