GitLab offers a number of tools through its web based UI that allow users to delete unprotected branches. But the process involved to delete remote GitLab branches from the client machine, and have those branch deletions pushed to the remote GitLab repository is a little bit more involved. The GitLab remote branch delete operation through the BASH shell of a Git client will be explained in this tutorial.
GitLab delete remote branch overview
The current GitLab repository contains a feature branch named fun_feature, which will be the target for the GitLab remote branch delete.
The required steps in a GitLab remote branch delete of a feature branch on the client side is as follows:
- Open a Terminal window in the gitlab-made-easy repository on the client machine
- Switch to the master branch through the use of the git checkout command
- Delete the branch locally
- Push to origin with the –delete flag
- Verify local and remote tracking branches are deleted with a branch listing command
Remote branch delete dangers
It should be noted that branch removals should be considered a permanent operation. In certain circumstances is it possible to use the Git reflog to restor deleted branches before they are garbage collected and removed permanently from the repository. But in most cases, when you perform a GitLab delete remote branch operation, the changes are permanent.
If you are an administrator and you are worried about a situation in which a developer or casual user might delete in important branch, such as the GitFlow develop, hotfix or release branches, you can take advantage of GitLab protected branches. Configured on the GitLab server, a protected branch cannot be deleted. Furthermore, rights to push or merge changes to a protected branch can be restricted to users in either the developer or maintainers role. There is also an option to completely remove the rights for anyone to merge or push to a protected branch, completely locking down the resource.
For users without rights to merge or push to a protected branch, integrating changes requires the creation of a merge request and subsequent review and approval from a user with elevated privledges.
Users will still be able to merge to protected branches on their local machine using standard Git commands, but those local updates cannot propagate to the cloud hosted or self-hosted GitLab server.
Remote GitLab branch removal commands
The commands issued to remove both the local branch, the local remote tracking branch and the branch on the GitLab server are as follows:
git checkout master git branch -d fun_feature git push origin --delete fun_feature git branch --all
Once the commands have been issued and no error codes are returned, you will notice that the fun_feature branch is no longer listed as a local or remote tracking branch. Furthermore, if you log into the GitLab server, you will see the remote GitLab branch removed there as well.
The source code and sample project used for these examples can be found on the gitlab-made-easy project page on GitLab.