How to delete branch gitlab

How to delete branch gitlab

How to Delete a Git Branch Both Locally and Remotely

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

In most cases, it is simple to delete a Git branch. You’ll learn how to delete a Git brach locally and remotely in this article.

TL;DR version

When to Delete branches

It is common for a Git repo to have different branches. They are a great way to work on different features and fixes while isolating the new code from the main codebase.

Repos often have a main branch for the main codebase and developers create other branches to work on different features.

Once work is completed on a feature, it is often recommended to delete the branch.

Deleting a branch LOCALLY

Git will not let you delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting. For example: git checkout main

The branch is now deleted locally.

Deleting a branch REMOTELY

The branch is now deleted remotely.

You can also use this shorter command to delete a branch remotely: git push :

For example: git push origin :fix/authentication

If you get the error below, it may mean that someone else has already deleted the branch.

Try to synchronize your branch list using:

If you found this tutorial helpful, our nonprofit has more than 8,000 no-nonsense tutorials like this one. All free, with no ads. Tell your friends. 😉

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Why can’t I delete a branch in a remote GitLab repository?

Part of GitLab Collective

6 Answers 6

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

To remove a local branch from your machine:

To remove a remote branch:

In your case the above statements would be:

To remove a local branch from your machine:

To remove a remote branch:

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

Edit (per comments by OP—I have not used GitLab): GitLab has a web interface with dropdowns. You need the one under the Settings view (not the Project view). Choose a branch in the Settings view under «Default branch» and click «Save changes» to set the current branch on the server.

Details

You have the right idea, but you have to remember that there are two repositories—two Gits—involved.

Does the current branch also need to be set in the remote directory?

Yes (well, «instead of» rather than «also»).

Of course, this assumes you can log in (see first point). If there is, say, a web interface that lets you change the current branch on the remote, it is going to have to do this git symbolic-ref operation for you.

Git Delete Branch How-To, for Both Local and Remote

July 19, 2021
Stay connected

Efficient branch management is essential for using Git to its fullest potential. This post is all about the Git delete branch operation. You’ll learn how to delete branches, both locally and in your remote repositories, and whether it’s possible to recover a deleted branch. Most importantly, you’ll learn what branches are under the hood to have a more solid understanding of what happens when you delete them.

This tutorial assumes you have Git installed and know your way around the command line. Also, make sure you have a GitHub account and are logged in.

Git Delete Branch: The TL;DR Version

If you just want to learn the correct incantation to delete branches in Git, we’ll start by offering the TL;DR version.

To delete a local branch in Git, you simply run:

You don’t use the git branch command to delete a remote branch. You use git push, even if that sounds weird. Here’s how you do it:

It’s like you’re pushing—sending—the order to delete the branch to the remote repository.

With that out of the way, we’ll now explore the deletion of branches in Git in some more depth.

Git Delete Branch: The Fundamentals

Let’s start by examining what branches are in Git and what’s happening when you delete them.

What Branches Are In Git?

Branching in Git is dead simple. Branches are named, movable references to commits. Imagine you create a repository and add three commits:

Now you create a new branch called “exp.” The branch currently points to the same commit, so the scenario looks like this:

You switch to the new branch and add two more commits to it. This is what your repo looks like now:

So, what would happen if you deleted the exp branch?

What Happens If I Delete a Git Branch?

When you delete a branch in Git, you don’t delete the commits themselves. That’s right: The commits are still there, and you might be able to recover them.

When you delete a branch, what happens depends on whether the branch was already merged. Let’s look at two scenarios.

Deleting a Branch With Merged Changes

Go back and take a look at the last diagram. As you can see, the main branch is behind exp. In this situation, merging is just a matter of moving the main reference forward until it catches up to exp. Let’s do that:

At this point, if we delete the exp branch, nothing changes. The commits were already integrated into the original branch:

Deleting a Branch With Unmerged Changes

Now let’s explore the scenario where the branch contains unmerged changes. Take a look at the diagram the depicts the moment after you’ve added the new commits to exp but haven’t yet merged them back to main:

If you go now and get rid of the exp branch, your repo will look like this:

As you can see, the commits are still there. But unless you have the hash of the last commit, you won’t be able to reach them. Why?

A commit has a reference to its parent—or parents—but not to its children. From the commit that main points to, you can only move backward, so there’s no way for you to reach the commits formerly pointed to by exp.

What if you delete a branch with unmerged commits by mistake? Are those changes lost for good? Not yet. There’s a way to recover them if you act quickly. More on that later.

How Do I Delete a Git Branch?

Let’s see an example.

Start by creating a repository and adding a commit to it:

echo hello world > file.txt

git add file.txt

Now you can create a second branch. Let’s call it new:

At this point, you could switch to the new branch, add some commits to it, then go back and merge it into main. The results would be the same, though. Let’s delete it right away:

After running the command, Git will let you know the branch was successfully deleted. It will also display the hash of the commit pointed to by the branch:

Deleted branch new (was c9bd965).

Now let’s try to delete a branch with unmerged changes. Create a new branch and switch to it:

While in this branch, add some commits:

Go back to the original branch:

git switch main

And try to delete newer:

It won’t work. Git will show you this message:

error: The branch ‘newer’ is not fully merged.

How to Delete a Branch Remotely

You’ll often need to delete a branch not only locally but also remotely. To do that, you use the following command:

Let’s look at an example. Start by creating a new repository on GitHub. After you’re done, you’ll create the local repo. Using the command line, create a new folder, access it, and start a new repo with one commit:

Now you can use the following commands to add the remote repository on GitHub as a remote for the local one (make sure you replace the actual URL with your own):

git remote add origin https://github.com/carlosschults/upgraded-eureka.git

Now let’s create a new branch, switch to it, and add a new commit:

Finally, you can push your new branch to GitHub:

You’ll see a result like this:

Go back to GitHub, and you’ll see your new branch there:

You’ll see a message showing that Git deleted the branch remotely:

How Do I Delete a Branch on GitHub?

It’s possible to delete branches on GitHub using the interface. You can choose to delete a branch when you merge a pull request. For instance:

You can also simply delete the branch at any time. To do that, repeat the operations from the last section to create a new local branch and push it to GitHub.

Then go back to your repo main page on GitHub, click on the button with the main branch name, and you’ll see this suspended menu:

Click on View all branches, like in the image above, and you’ll be taken to a page where you can see the list of all branches.

To delete the new-feature branch, just click on the trash can icon next to its name. After deleting the branch, it will still be listed for a short while, and you’ll be able to recover it by clicking on the Restore button:

Can I Recover a Deleted Branch in Git?

Is it possible to recover local deleted branches in Git? Well, sort of. As you’ve seen, branches are simply references that point to a commit. What you want to do is make that commit reachable again. For that, you’ll need its identifier.

As you’ve seen, when you delete a branch, Git displays the hash of the commit the branch pointed to. If you write that down, you can later use it to access that commit and create a new branch from that point:

What if you didn’t write it down? There’s still hope for you in the name of a command called reflog.

In a nutshell, git reflog allows you to see the reference logs. At its most basic form, you can use it to see the list of commits that the HEAD reference pointed to over time.

As an example, let’s delete a branch locally:

Now, suppose I made a terrible mistake and the branch wasn’t supposed to be deleted. I can use the git reflog command:

Having recovered the commit hash, I can then do (remember your actual hash number will be different):

git checkout 984a656

Finally, a caveat: git reflog will only save you if the HEAD reference has pointed to the commit you want to recover. You might have scenarios where the desired commit isn’t actually in the reflog, so there will be no way for you to recover its hash.

Git Delete Branch: Do It When It’s Time

Before wrapping up, there’s a final question we need to answer: When is it OK to delete branches in Git?

In general, it’s safe to delete branches after they’ve been merged. It might be OK to delete unmerged branches—for instance, after an experiment that went nowhere. You must be aware of the consequences, though: The commits might become unreachable.

Before parting ways, here’s a final thought: Just because you can, doesn’t mean you should. Sure, Git’s powerful branching capabilities might feel fantastic, but using a branching strategy that relies too much on branches—particularly long-lived ones—can get you into trouble.

An alternative is to use a workflow that doesn’t rely on branching—for example, trunk-based development. That, along with a mechanism such as feature flagging, allows teams to go fast, keeping things simple and facilitating CI and CD.

This post was written by Carlos Schults. Carlos is a consultant and software engineer with experience in desktop, web and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control and code quality.

How to fully delete remote Git branches from GitHub

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

Community driven content discussing all aspects of software development from DevOps to design patterns.

But if that local Git branch was created through a pull from are remote repo like GitHub or BitBucket, how do you delete the remote branch as well? And for that matter, how do you delete the reference to the remote branch in your local Git repo?

There are many ways to delete a remote Git branch that lives on GitHub or BitBucket. If you have administrative access to the GitHub or BitBucket landing pages for your repo, there are plenty simple, of point-and-click deletion options for unprotected branches. Alternatively, you can issue either of the following Git commands to delete a remote branch:

The only problem is, depending on which version of Git you are using, your local repo may still retain a remote reference that points to the remote branch you just deleted. This is known as Git’s remote tracking branch.

So a thorough branch removal strategy may requires the explicit deletion of this remote tracking branch as well.

You can delete a remote tracking with the following git branch command:

However, if the branch has already been deleted from the GitHub or BitBucket server, a simpler approach is to call the git fetch command with the prune option. The prune option removes any remote tracking branch in your local repository that points to a remote branch that has been deleted on the server.

With the local branch deleted, the remote tracking branch deleted, and all instances of the remote git branch deleted as well, the remote Git branch should be gone forever.

In review, the steps to delete remote Git branches are:

Remember, Git is a fully distributed system, and repositories may be cloned any number of times. Most organizations use GitHub or BitBucket as a single source of truth, but there may be any number of clones of a given Git repo. While the commands discussed here will delete a branch from your local repository, and the Git instance you use as a central repository, there is always a chance that the branch still exists on a clone of the repository of which you are not aware.

Microsoft’s Azure Advisor service offers recommendations based on five categories. Learn these categories and the roles they play.

Researchers with Palo Alto Networks took the stage at Black Hat to explain how configurations and system privileges in Kubernetes.

How to perform a GitLab delete remote branch operation on any repository

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

Community driven content discussing all aspects of software development from DevOps to design patterns.


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.

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:

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

The remote GitLab branch to delete is named feature_fun

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.

How to delete branch gitlab. Смотреть фото How to delete branch gitlab. Смотреть картинку How to delete branch gitlab. Картинка про How to delete branch gitlab. Фото How to delete branch gitlab

Both local and remote GitLab branches must be deleted.

A developer can use the following commands to remove the local branch, the local remote tracking branch and the branch on the GitLab server:

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.

Microsoft’s Azure Advisor service offers recommendations based on five categories. Learn these categories and the roles they play.

Researchers with Palo Alto Networks took the stage at Black Hat to explain how configurations and system privileges in Kubernetes.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *