2
Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi)

Hii, I am new to GitHub and I want to know that what is this, and why it is showing every time and how can I remove it? (add-hemang-joshi) is in green colour in GitBash. See the image as well.

I tried using:

git branch -D add-hemang-joshi

but didn't work and showed an error viz.

error: Cannot delete branch 'add-hemang-joshi' checked out at 'C:/Users/7386-856
15SG 2913642'

My gitBash looks like this

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
Hemang Joshi
  • 158
  • 1
  • 5
  • 13

3 Answers3

5

You couldn't delete the branch you are on currently. So checkout to the other branch and then delete the required branch.

git checkout master 

The above command switches to the master branch.

git branch -d add-hemang-joshi

The above command would delete the add-hemang-joshi branch.

Adding to the above

The -d option will delete the branch only if it has already been pushed and merged with the remote branch. The -D is to force delete a branch even if it hasn't been pushed or merged yet.

Krithick S
  • 408
  • 3
  • 15
3

We can't delete a branch sitting on that branch. So, you need to checkout to another branch then delete the branch.

$ git checkout master

If master branch does not exist in your local then create and checkout to a new branch (e.g. add-hemang-joshi_2):

$ git checkout -b add-hemang-joshi_2

Delete the branch add-hemang-joshi:

$ git branch -D add-hemang-joshi  # delete the branch

$ git branch                      # check if the branch is deleted
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • I tried to do this, but I encountered an error error: pathspec 'master' did not match any file(s) known to git – Hemang Joshi Sep 04 '21 at 10:59
  • 1
    @HemangJoshi try `git fetch` and then make a checkout. A post related to your error https://stackoverflow.com/questions/5989592/git-cannot-checkout-branch-error-pathspec-did-not-match-any-files-kn – Krithick S Sep 04 '21 at 11:06
  • Still, no change ```Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi) $ git fetch Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi) $ git checkout master error: pathspec 'master' did not match any file(s) known to git ``` – Hemang Joshi Sep 04 '21 at 11:12
  • 1
    @HemangJoshi ok, could you try create and checkout to a new branch: $ git checkout -b add-hemang-joshi_2 $ git branch -D add-hemang-joshi – Sajib Khan Sep 04 '21 at 12:20
  • 1
    @HemangJoshi did you set the origin with the remote URL? what is the output of $ git branch -a ? – Sajib Khan Sep 04 '21 at 12:23
  • 1
    @HemangJoshi: you need to give `git checkout` the name of some branch that it understands. This would normally be the same name as some *remote-tracking name* that `git branch -r` would print out, minus the `origin/` part. Your `origin` repository might have `main` instead of `master`, for instance. – torek Sep 04 '21 at 18:17
  • @SajibKhan I created that new branch and switched to it. But when I did Delete operation for first branch, it was showing an error ```error: branch 'add-hemang-joshi' not found. ``` git branch -a is not even showing any output ```Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi_2) $ git branch -a Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi_2) $``` – Hemang Joshi Sep 05 '21 at 09:17
  • @torek git branch -r is not showing any kind of output. ```Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi_2) $ git branch -r Hemang Joshi@SOLO MINGW64 ~ (add-hemang-joshi_2) $``` – Hemang Joshi Sep 05 '21 at 09:21
  • @HemangJoshi since you are getting the following error that means your branch `add-hemang-joshi`has been deleted already! `"error: branch 'add-hemang-joshi' not found"` – Sajib Khan Sep 05 '21 at 14:49
  • @SajibKhan Yeah, I guess it's deleted!!, but now I cannot delete this add-hemang-joshi-2 and I cannot even checkout to the master branch. So what shall I do? – Hemang Joshi Sep 06 '21 at 00:27
  • @HemangJoshi you need to set a remote like `$ git remote add origin ` then `$ git fetch` then `$ git checkout master` then try to delete the branch in same way! – Sajib Khan Sep 06 '21 at 05:01
  • @SajibKhan It worked!! I know stackoverflow doesn't recommend saying thanks, but I need to say. Thanks a lot!! – Hemang Joshi Sep 06 '21 at 10:11
2

You cannot delete the branch on which you currently are.

First switch to another branch.

Gaël J
  • 11,274
  • 4
  • 17
  • 32