Deleting a local branch with Git

Solution 1:

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

Solution 2:

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.

Solution 3:

Ran into this today and switching to another branch didn't help. It turned out that somehow my worktree information had gotten corrupted and there was a worktree with the same folder path as my working directory with a HEAD pointing at the branch (git worktree list). I deleted the .git/worktree/ folder that was referencing it and git branch -d worked.

Solution 4:

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

Solution 5:

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

git branch -d my-branch-name

without force delete, you can delete that branch