how to delete a branch from a repo in sourcetree with only 1 branch?
I have SourceTree installed on my local environment. I’m working within a shared collection of git repos for at an enterprise. Looks like the admin created a new repo RepoY and we previously only had 1 repo (RepoX). I accidentally created BranchZ in RepoY and I need to delete it.
I selected BranchZ through SourceTree under RepoY, right-clicked and I was able to “Remove” it but now it displays with a red minus sign to the left. I tried to “Delete” the branch but SourceTree returns the following error: “error: Cannot delete the branch ‘BranchZ’ which you are currently on.”
I’m not able to switch branches within the repo because BranchZ is the only branch in the repo. If I double-click a different repo in SourceTree then I can only delete RepoY but not BranchZ within RepoY. Is there a way to do what I’m trying to do through SourceTree or is this a limitation of SourceTree?
2 Solutions collect form web for “how to delete a branch from a repo in sourcetree with only 1 branch?”
When you create a git repository using
git init, master branch will be created by default. I think in your case master branch may be deleted by some mistake, or you accidently renamed it. So, in this case, what you can do from SourceTree is,
- Create a branch called
Or from command line,
cd <repo_dir> git branch master git checkout master git branch -d BranchZ
You are not allow to delete the branch which you are on, firstly you have checkout to another branch since it is the only branch you can just simply checkout master branch(every repo has master branch if it wasn’t deleted). Then you are able to delete but be sure you are in correct repo.
git checkout master git git push origin :<branchName> (it deletes local and remote branch in case of you already pushed commits)