Git

How to Remove a GitLab Branch from Git

Like other Remote host servers, GitLab also has the concept of branches for working with different features and issue fixes while isolating the new code from the main codebase. Users can create multiple branches for each development module. By default, the repositories have a β€œmain” branch for the codebase. However, users can make new branches to work on several features. Sometimes users are required to delete the branches that can be unprotected. For that purpose, they can use Git’s different commands.

This blog will describe the method of removing a GitLab branch from Git.

How to Remove a GitLab Branch from Git?

To remove the GitLab branch from Git locally and remotely, check out the provided steps:

  • Navigate to the Git root directory.
  • List all existing remote branches using the β€œgit branch -r” command.
  • Select the target branch and execute the β€œgit push <remote-name> –delete <remote-branch>” command.

Step 1: Move to Git Root Directory

Initially, execute the β€œcd” command along with the Git root path and move to it:

cd "C:\Users\nazma\Git\Git"

Step 2: List All Remote Branch

Then, display all GitLab remote branches by using the β€œgit branch” command and β€œ-r” option that indicates the remote:

git branch -r

As you can see, all the remote branches have been listed and we have selected the β€œorigin/beta” branch that we want to delete:

Step 3: Delete Remote Branch

To remove a remote branch, execute the following command:

git push origin --delete dev

Here:

  • β€œorigin” is our remote URL name.
  • β€œ–delete” option is used for deleting purposes.
  • β€œdev” is the target branch.

According to the below-given output, the remote branch has been removed:

Step 4: Verification

Lastly, use the provided command for verifying whether the specified remote branch has been removed or not:

git branch -r

That’s it! We have described the way for removing the GitLab branch from Git.

Conclusion

To delete the GitLab branch from Git, first, navigate to the Git root directory and list all existing remote branches by running the β€œgit branch -r” command. Then, choose one of them and run the β€œgit push <remote-name> –delete <remote-branch>” command. This tutorial described removing a GitLab branch from Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.