2

I created a local empty branch with:

mkdir test
cd test
git checkout -b branch_local

I am not able to delete it with:

git branch -D branch_local

I get

error: Cannot delete branch 'branch_local' checked out at 'D:/test'

Please advise.

4
  • 1
    You need to check out a different branch first, but are you trying to delete the repository or the branch? Commented Apr 2, 2018 at 10:46
  • 1
    Are you trying to delete the whole repository or the branch? Your question and the context is very ambiguous. Commented Apr 2, 2018 at 10:48
  • 1
    The commands you listed do not create or delete a repository. They operate on branches. Read more about the Git concepts in the Git book. Commented Apr 2, 2018 at 10:49
  • Thank you, I have edited the question. It is a branch creation and deletion, not repo. Commented Apr 3, 2018 at 2:47

2 Answers 2

4

Check out on different branch before deleting the branch to be deleted. For example :

STEP 1: git checkout master

STEP 2: git branch -D branch_local

branch_local is the name of branch to be deleted.

Sign up to request clarification or add additional context in comments.

1 Comment

STEP 1 will fail if the pwd is not connected to a repo.
0

If you want to remove your repository and keep the files in it, you can remove the .git/ directory in test/. WARNING: This will destroy your history!

If you want to remove the repository and the files, you can remove the directory test/. WARNING: This will destroy the history AND the files!

If you want to destroy the new branch and switch to another one (this is what I think you want to do), you need to check out that branch first. If it exists, you run git checkout <branch_name> && git branch -D branch_local. If the other branch doesn't exist, you add the -b flag: git checkout -b <branch_name> && git branch -D branch_local WARNING: The history in branch_local will be lost unless you keep the hash of the commit and prevent any garbage collections.

If you want to remove the branch without checking out a new one, you can remove the file .git/refs/heads/branch_local. Git will still think that you have this branch checked out since that name is in the .git/HEAD file, and this file is needed for the repository to be valid.

Note that if you created the new branch from an initial commit, the file .git/refs/heads/branch_local won't exist, and nor will the branch. The name is only the name of the branch to be created if you make a commit, and this name is stored in the .git/HEAD file, which contains the line

ref: refs/heads/branch_local

Thus you can edit the file directly, or do it using the git commands above.

In summary, there has to be a name of a branch in the .git/HEAD file or the repository is invalid, regardless of whether the branch actually exists.

3 Comments

Thank you! I didn't find a file: .git/refs/heads/branch_local. So I removed the ref: refs/heads/branch_local from .git/HEAD. I am wondering if there is a command to delete a branch without checking out a new one.
I think you disregarded the last paragraph: there has to be a name of a branch in that file. If you run git status in the repository, you should get an error message now. I'd suggest you put that line back, but replace the branch name with another one.
Please re-read my previous comment. There was a line in the HEAD file which I deleted in order to delete the local branch. However, I could not find a file that was named after the local branch about which you had mentioned in para 4, sentence 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.