41

I'm pretty new to using git, and I use it to contribute to the AOKP Android ROM. I've successfully created a few branches, modified the code, and uploaded the commits that have gotten merged on the remote end. But in my local repository, those branches are still showing up (even though they show up as having no changes). The problem is that when I created those branches, I did so right from the subfolder that needed to be modified, so I don't have any "higher" branches to go to. And because of that, I can't delete those branches -- git tels me error: Cannot delete the branch 'MyMods' which you are currently on.

So what can I do to get rid of those branches?

user496854
  • 6,461
  • 10
  • 47
  • 84
  • Git doesn't allow this because it's a bit intelligent than these folk: https://www.youtube.com/watch?v=zE8el_qscvE – sampathsris Jul 13 '15 at 07:11

4 Answers4

56

Checkout a different branch first, before deleting it:

git checkout master
git branch -d MyMods

Also, branches have nothing to do with folders. Git always tracks the whole repository at once, with all its folders and files. A branch is nothing else than a pointer to a single commit, or snapshot, in the history of the repository.

poke
  • 369,085
  • 72
  • 557
  • 602
  • I understand the diff between what git does and physical folders. I just hate seeing a warning every time I run "repo sync" that my branch is a number of commits behind, and since I'm done with it, I want to get rid of it. But if I checkout a master branch, won't I run into the same problems because then the master branch will just be hanging there with no commits? – user496854 Sep 27 '12 at 03:26
  • I folloed this iriginal tutorial :http://rootzwiki.com/topic/22424-aokp-gerrit-tutorial, and it just had me create a branch right in the app that I was modding. So, do you mean that I have to check out the whole AOKP tree, and how will that affect my normal repo syncing? – user496854 Sep 27 '12 at 03:27
  • `git checkout master` (or any other branch) does not give any response, the command seems to take forever. I think that one branch is broken somehow, so I want to remove it. – Jānis Elmeris Nov 02 '15 at 09:16
5

Yes just checkout another branch(maybe master) and then:

git checkout master
git branch -d thebran
abatishchev
  • 98,240
  • 88
  • 296
  • 433
slash28cu
  • 1,614
  • 11
  • 23
0

But in my local repository, those branches are still showing up (even though they show up as having no changes).

Branches will show up in your local repo unless you delete them. They don't disappear when you push them.

The answers above tell you how to delete a branch. However, I would add that using the -D option is a bit more powerful. This option deletes the branch regardless of its (un)merged status:

git branch -D branchName

It's the atomic bomb of branch deletion. Use with care.

gitlinggun
  • 108
  • 6
  • the original post specifically states that it won't let me delete the branch because i'm currently on it, and there are no higher branches to move to. So how does your answer help? – user496854 Sep 27 '12 at 15:53
  • You will need to switch branches first - forgot to add that. And my comment also points to the other answer which states that you can only delete a branch you are not on. – gitlinggun Sep 27 '12 at 16:31
  • What do you mean by "higher branches"? This means nothing in the git world. – gitlinggun Sep 27 '12 at 16:34
  • of course it does. you can have branch 1 right off the trunk, then have branch 2 be a few folders deep inside branch 1. So, branch q is "higher" then branch 2 if you think of it as a tree. Git won't allow you to delete branch 2 unless you switch to a different branch first, and I can't do that because I have no "higher" branches. – user496854 Sep 30 '12 at 14:26
  • 1
    I think you may be getting confused between folders and branches. You may want to read Pro Git or Ry's Friendly Guide to Git to get a better understanding of how git works with hierarchy. I'm thinking the question has been answered and we're just getting off-topic now. Good luck! – gitlinggun Oct 02 '12 at 10:31
  • I'm not getting confused. You can't delete a branch that you're currently on, and I think that you need to read that guide yourself if you don't know something as basic as that. The original post explains it perfectly, and I need some direction on deleting a branch that I no longer need. – user496854 Oct 03 '12 at 16:01
0

If the branch you cannot delete is a worktree then you may need to delete that worktree first:

rm -r MyMods
git worktree list    # shows MyMods
git worktree prune
git worktree list    # doesn't show MyMods
git branch -d MyMods
101
  • 8,514
  • 6
  • 43
  • 69