How can I delete (or merge) a local Git branch that I'm currently on?

51,388

Solution 1

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.

Solution 2

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

git checkout master
git branch -d thebran
Share:
51,388
user496854
Author by

user496854

Updated on August 29, 2020

Comments

  • user496854
    user496854 over 3 years

    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
    user496854 over 11 years
    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
    user496854 over 11 years
    I folloed this iriginal tutorial :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
    user496854 over 11 years
    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?
  • gitlinggun
    gitlinggun over 11 years
    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
    gitlinggun over 11 years
    What do you mean by "higher branches"? This means nothing in the git world.
  • user496854
    user496854 over 11 years
    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.
  • gitlinggun
    gitlinggun over 11 years
    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!
  • user496854
    user496854 over 11 years
    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.
  • jinyong lee
    jinyong lee over 8 years
    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.