Deleting branch with repo abandon and git branch -D

mrutyunjay picture mrutyunjay · May 23, 2013 · Viewed 12.6k times · Source

I am using git and git-repo for my project. I see when I try to delete my local branch which I am currently on using git command

git branch -D branch_name

It shows me error which I am expecting, as we can't delete current branch.

But if I use repo command

repo abandon branch_name

I am able to delete the current branch. So my question is what command is repo using internally to delete the branch?

Answer

VonC picture VonC · May 23, 2013

The abandon.py subcmd calls project.AbandonBranch, which includes:

head = self.work_git.GetHead()
if head == rev:
  # We can't destroy the branch while we are sitting
  # on it.  Switch to a detached HEAD.
  #
  head = all_refs[head]

  revid = self.GetRevisionId(all_refs)
  if head == revid:
    _lwrite(os.path.join(self.worktree, '.git', HEAD),
            '%s\n' % revid)
  else:
    self._Checkout(revid, quiet=True)

In other words, it makes sure to not be on the branch you are deleting, even if that mean setting up a detached HEAD (by a checkout of a SHA1 'revid').