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.
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 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.
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.
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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With