Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a local branch with Git

Tags:

git

I've googled and there are several very long threads on this topic and none of them seem to help. I think I'm doing something wrong. I have a branch called Test_Branch. When I try to delete it using the recommend method, I get the following error:

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.

like image 447
Bob Wakefield Avatar asked Jan 05 '17 18:01

Bob Wakefield


People also ask

How do I delete a local only branch?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

What happens if I delete a local branch in git?

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.

How do I delete a local and remote branch?

So, to delete the remote branch AND locally-stored remote-tracking branch in one command, just use git push origin --delete <branch> . Then, you just need to delete the local branch with git branch -D branch . That covers the deletion of all 3 branches with only 2 commands.

Does deleting a local branch affect remote?

Local branches are branches on your local machine and do not affect any remote branches. git branch is the command to delete a branch locally. -d is a flag, an option to the command, and it's an alias for --delete .


2 Answers

Switch to some other branch and delete Test_Branch, as follows:

$ git checkout master $ git branch -d Test_Branch 

If above command gives you error - The branch 'Test_Branch' is not fully merged. If you are sure you want to delete it and still you want to delete it, then you can force delete it using -D instead of -d, as:

$ git branch -D Test_Branch 

To delete Test_Branch from remote as well, execute:

git push origin --delete Test_Branch 
like image 64
Arpit Aggarwal Avatar answered Sep 21 '22 23:09

Arpit Aggarwal


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.

like image 44
Randy Leberknight Avatar answered Sep 21 '22 23:09

Randy Leberknight