How to delete local branch git

How to delete local branch git

Git Delete Branch – How to Remove a Local or Remote Branch

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

Git is a popular version control system and an essential tool in a web developer’s toolkit.

Branches are a powerful and integral part of working with Git.

In this article, you will learn the basics about how to remove local and remote branches in Git.

What are Branches in Git?

A branch is a pointer to a commit.

Git branches are a snapshot of a project and its changes, from a specific point in time.

Branching allows you to create new, independent versions of the original main working project. You might create a branch to edit it to make changes, to add a new feature, or to write a test when you’re trying to fix a bug. And a new branch lets you do this without affecting the main code in any way.

So to sum up – branches let you make changes to the codebase without affecting the core code until you’re absolutely ready to implement those changes.

This helps you keep the codebase clean and organized.

Why Remove Branches in Git?

So you’ve created a branch to hold the code for a change you wanted to make in your project.

You then incorporated that change or new feature into the original version of the project.

That means you no longer need to keep and use that branch, so it is a common best practice to delete it so it doesn’t clutter up your code.

How to Delete a Local Branch in Git

Local branches are branches on your local machine and do not affect any remote branches.

The command to delete a local branch in Git is:

Let’s look into this in a bit more detail with an example.

To list out all the local branches, you use the following command:

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

I want to delete the test2 branch, butit is not possible to delete a branch you are currently in and viewing.

If you try to do so, you’ll get an error that will look something like this:

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

So before deleting a local branch, make sure to switch to another branch that you do NOT want to delete, with the git checkout command:

Here’s the output:

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

Now I can delete the branch:

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

The command for deleting a local branch that we just used doesn’t work in all cases.

This is because the commits are not seen by any other branches and Git is protecting you from accidentaly losing any commit data.

If you try to do this, Git will show you an error:

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

But note that you should use this command should with caution, as there is no prompt asking you to confirm your actions.

Use it only when you are absolutely sure you want to delete a local branch.

If you didn’t merge it into another local branch or push it to a remote branch in the codebase, you will risk losing any changes you’ve made.

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

How to Delete a Remote Branch in Git

Remote branches are separate from local branches.

They are repositories hosted on a remote server that can be accessed there. This is in comparisson to local branches, which are repositories on your local system.

The command to delete a remote branch is:

Now, let’s see an example of how to go about deleting a remote branch.

To view any remote branches, you use this command:

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

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

I want to delete the remote origin/test branch, so I use the command:

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

The origin/test remote repository is no longer there:

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

Conclusion

You now know how to delete local and remote branches in Git.

If you want to learn more about Git, you can watch the following courses on freeCodeCamp’s YouTube channel:

Git FAQ

Frequently asked questions around Git and Version Control.

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

How do I delete a local branch in Git?

In some cases, Git might refuse to delete your local branch: when it contains commits that haven’t been merged into any other local branches or pushed to a remote repository.
This is a very sensible rule that protects you from inadvertently losing commit data.

If you want to delete such a branch nonetheless (e.g. because you’ve programmed yourself into a dead end and produced commits that aren’t worth keeping) you can do so with the «-D» flag:

This will force deletion of the branch, even if it contains unmerged / unpushed commits. It goes without saying: please be careful with this command!

Can I undo deleting a branch?

In most cases, if you don’t let too much time pass, you can restore a deleted branch.

If you’re working with Git on the Command Line, you should take a look at a Git tool called «Reflog». Learn more about this in our free First Aid Kit for Git video series.

How do I delete a remote branch in Git?

To delete a remote branch, you need to use the «git push» command:

Learn More

Get our popular Git Cheat Sheet for free!

You’ll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.

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

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That’s why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.

Deleting a local branch with Git

Cannot delete branch ‘Test_Branch’ checked out at ‘[directory location]’.

I get no other information besides that. I can blow away the remote branch easy but the local branch won’t go away.

11 Answers 11

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

To delete Test_Branch from remote as well, execute:

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

You probably have Test_Branch checked out, and you may not delete it while it is your current branch. Check out a different branch, and then try deleting Test_Branch.

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

If you have created multiple worktrees with git worktree, you’ll need to run git prune before you can delete the branch

In my case there were uncommitted changes from the previous branch lingering around. I used following commands and then delete worked.

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

If you run into this problem where you have checkedout and not able to delete the branch and getting this error message

Then check the branch you are currently in by using git branch

If the branch you are trying to delete is your current branch, you cannot delete the same. Just switch to the main or master or any other branch and then try deleting

git checkout main or master

Most junior programmers that faced

Cannot delete branch ‘my-branch-name’

Because they are already on that branch.

It’s not logical to delete the branch that you are already working on it.

Just switch to another branch like master or dev and after that delete the branch that you want:

git checkout dev

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

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

Like others mentioned you cannot delete current branch in which you are working.

In my case, I have selected «Test_Branch» in Visual Studio and was trying to delete «Test_Branch» from Sourcetree (Git GUI). And was getting below error message.

Cannot delete branch ‘Test_Branch’ checked out at ‘[directory location]’.

Switched to different branch in Visual Studio and was able to delete «Test_Branch» from Sourcetree.

I hope this helps someone who is using Visual Studio & Sourcetree.

The magic is to remember here that these are references and MUST be deleted via

Took a while to uncover my mistake and only the fact that TortoiseGit could do it led me on the right path when I was stuck. Google DID NOT deliver anything useful despite several different approaches. (No that I’ve found what I needed there’s also git: How to delete a local ref branch?, which did NOT show up as I had forgotten the ‘ref’ bit about these branches being references.)

Hope this helps someone else to get unstuck when they’re in the same boat as me, trying to filter/selectively remove branches in a repo where some of them are present due to previous git remote add commands.

In a shell script this then might look like this:

How do I delete a local branch on Github Desktop?

Last week I upgraded my Windows Github to Github Desktop.

It is certainly a lot faster than that last update they made for Github Windows. It also has a nice little visual timeline of commits and maybe I am being dumb, but the ability to delete local branches seems to be gone.

Before it was under “manage branches” (also gone). Any idea?

Update:

In v3.0.5 they have added the option to delete local branches from the gear menu. How to delete local branch git. Смотреть фото How to delete local branch git. Смотреть картинку How to delete local branch git. Картинка про How to delete local branch git. Фото How to delete local branch git

8 Answers 8

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

On Mac OSX:

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

On Windows:

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

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

Here’s the answer I got from github CS when I contacted them about this:

Thu 2015-08-13 3:38 PM Hi Daniel, Sorry about that! We’ll be adding the option to delete branches back in an upcoming release. For now however, here’s how you can delete a branch from the Git Shell: Hit

I’ve noticed there is a delay (about 2 or 3 minutes) between deleting the local branch in the shell and git desktop realizing the change.

UPDATE

In 3.0.5 They added Delete the current branch from the gear menu.

git: How to delete a local ref branch?

I have bit of a difficulty in deleting a local ref branch(refs/notes/origin/commits). I was able to delete the branch(refs/notes/origin/commits) in the remote repository using the command

but when i try to delete the same branch on my local repository, i get the below error

3 Answers 3

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

to delete the local branch.

to delete the ref.

You can also hard-delete it as mentioned in other answers with

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

You have notes in your git repository, you can delete one note with

For deleting of all notes you have to remove the ‘notes’ directory

or you can use the git update-ref command.

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

Not the answer you’re looking for? Browse other questions tagged git git-notes or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

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

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

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