Search code examples
gitbranchgit-branch

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


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


Solution

  • 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.