GitLab offers several tools through its web-based UI that allow users to delete unprotected branches. But the process to delete remote GitLab branches from the client machine and have those branch deletions pushed to the remote GitLab repository is a bit more involved.
Let’s explore the GitLab delete remote branch operation through the BASH shell of a Git client.
GitLab delete remote branch overview
In this example, the current GitLab repository contains a feature branch named fun_feature, which will be the target for the GitLab remote branch delete.
A developer can follow these steps to have GitLab delete a remote branch of a feature branch on the client side:
- Open a Terminal window in the gitlab-made-easy repository on the client machine;
- Switch to the master branch via the ‘git checkout’ command;
- Delete the branch locally;
- Push to origin with the –delete flag; and
- Verify local and remote tracking branches are deleted with a ‘branch listing’ command.
Dangers associated with remote branch deletions
Developers need to know that branch removals should be considered a permanent operation. In certain circumstances it’s possible to use the Git reflog to restore deleted branches before they’re garbage collected and removed permanently from the repository, but in most cases a GitLab delete remote branch operation results can’t be undone.
If an administrator is worried about a situation where a developer or casual user might delete an important branch — such as the GitFlow develop, hotfix or release branches — they can take advantage of GitLab protected branches. A protected branch is configured on the GitLab server and can’t be deleted. Furthermore, an administrator can restrict rights to push or merge changes to a protected to users in either the developer or maintainers role. There’s also an option to completely remove the rights for anyone to merge or push to a protected branch, which completely locks down the resource.
If a user without rights wants to merge or push to a protected branch, it will create a merge request and subsequent review and approval from a user with elevated privileges.
Users will still be able to merge to protected branches on their local machine with standard Git commands, but those local updates cannot propagate to the cloud hosted or self-hosted GitLab server.
Remote GitLab branch removal commands
A developer can use the following commands to remove the local branch, the local remote tracking branch and the branch on the GitLab server:
git checkout master git branch -d fun_feature git push origin --delete fun_feature git branch --all
Once the commands are issued and no error codes are returned, a developer will notice that the fun_feature branch is no longer listed as a local or remote tracking branch. Furthermore, if they log into the GitLab server, they 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.