Home > Blockchain >  I can't delete a branch with "-d" only with "--delete"
I can't delete a branch with "-d" only with "--delete"

Time:10-07

I'm a beginner and doing some tests with branch creation and deletion:

git checkout -b quick-test
Switched to a new branch 'quick-test'

git branch
  main
* quick-test
  –d

git checkout -d quick-test
HEAD is now at 4df2fe6 Merge pull request #1 from tadm123/feature-readme-instructions

git branch
* (HEAD detached at refs/heads/quick-test)
  main
  quick-test
  –d

The only way it works is to use the full command --delete:

git branch --delete quick-test
Deleted branch quick-test (was 4df2fe6).

git branch
* (HEAD detached at 4df2fe6)
  main
  –d

Any advise into what I'm doing wrong would be appreciated

CodePudding user response:

To delete a branch, use git branch -d. To create a branch and check out, use git checkout -b. git checkout -d detaches your HEAD, effectively checking out a commit directly without being on a branch, it does not delete a branch.

-d, --detach

Rather than checking out a branch to work on it, check out a commit for inspection and discardable experiments. This is the default behavior of git checkout <commit> when <commit> is not a branch name. See the "DETACHED HEAD" section below for details.

If you look at your git branch output, you have a branch named "–d" (looks very similar to "-d"). Delete it to avoid any future confusion and problems (git branch -d –d).

To delete branch quick-test: git branch -d quick-test.

CodePudding user response:

Short answer: git branch -d <branch> and git branch --delete <branch> are both fine.


So what happened?

When you first tried to delete quick-test, you used (probably by mistake) the character (en dash) instead of the regular hyphen (-).

git considers it a normal character and does not detect your (intended) -d option, creating a –d branch instead!


How to recover?

Delete the useless branch with

git branch -d –d

then make sure your interface or anything else isn't silently replacing your hyphens.

  • Related