49

I have recently cloned a repo of our development code branch in my system:

git clone https://gitserver.com/product

After the clone was successful I get the below status on query:

$ git branch
* develop

I realized that now this branch needs to be deleted, hence:

$ git checkout develop
Already on 'develop'
Your branch is up-to-date with 'origin/develop'.

$ git branch -d develop
error: Cannot delete branch 'develop' checked out at 'C:/work/test'

I am not sure whether we should try a GIT command or Unix command 'rm -rf' to delete a local develop branch repository? Lastly why no one can delete 'develop' branch.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 1
    Don't think you can delete a branch which are checked out at. – ggbranch Nov 10 '17 at 05:17
  • 1
    As @WeeYou said, you cannot delete a branch you are "on" (Git says `Already on 'develop'` so you are "on" branch `develop`). Check out some other branch, such as `master`, first. – torek Nov 10 '17 at 05:19
  • Then how can I completely remove the branch I cloned - is it that I need to run the Unix rm -rf command? – Programmer Nov 10 '17 at 05:26
  • 1
    You didn't clone a branch, you cloned a repository, the repository *contained* that branch. Can you elaborate what you're trying to do? Given your suggestion to use `rm -rf` , do you mean you want to get rid of the entire clone (repository)? If so then yes, you can remove it from disk in any means available to you depending on OS and file system. – Lasse V. Karlsen Nov 10 '17 at 08:43

3 Answers3

81

You cannot delete the branch you are currently on.

Try creating a new branch

git checkout -b new-branch

Then delete the develop branch

git branch -d develop
Nandu Kalidindi
  • 6,075
  • 1
  • 23
  • 36
  • Then how can I completely remove the branch I cloned - is it that I need to run the Unix rm -rf command? – Programmer Nov 10 '17 at 05:25
  • You mean from the remote as well? – Nandu Kalidindi Nov 10 '17 at 05:26
  • No the cloned local repo only - I have cloned https://gitserver.com/product repo under C:/work/test folder - need to delete the cloned repo completely – Programmer Nov 10 '17 at 05:28
  • 1
    You want to delete the cloned repo? Then why even bothering deleting the branch? `rm -r` would delete the cloned repo. So, you cloned the repository, deleted the branch and then deleted the repository? That serves no purpose. – Nandu Kalidindi Nov 10 '17 at 05:32
  • Well no - I actually locally cloned - did some work - pushed the code and after project work is over I need to free the space and since it is a GIT repo I thought there would be GIT command to *completely* delete local cloned codebase if of no project use anymore my side. – Programmer Nov 10 '17 at 05:34
  • 1
    If you want to free up space just delete the cloned directory just like how you would delete a directory from your machine. `rm -r` or remove from the file explorer. – Nandu Kalidindi Nov 10 '17 at 05:38
  • 1
    To delete a branch forcefully use git branch -D branchName (note the capitalised D) – jburtondev Nov 10 '17 at 14:51
1

Adding to Nandu Kalidindi's answer:

When you clone a repo, it will always have a master branch. This master branch, shouldn't be deleted. And if you want to delete it anyway, you must push another branch before, so git will recognize the new pushed branch as the new master branch.

So, in your case, if you want to delete the repo you should try a UNIX command (rm -rf).

Víctor López
  • 819
  • 1
  • 7
  • 19
0

The branch which you are on currently cannot be deleted so create a new branch

git checkout -b new_branch

then delete the branch you want to delete

git branch -d develop

Deeksha Sharma
  • 3,199
  • 1
  • 19
  • 16