6

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?

Charles
  • 48,924
  • 13
  • 96
  • 136
mrutyunjay
  • 4,840
  • 5
  • 22
  • 34
  • 2
    If anyone is interested in doing research on this, you can find the source code for the `abandon` command at https://gerrit.googlesource.com/git-repo/+/master/subcmds/abandon.py –  May 23 '13 at 05:48

1 Answers1

4

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').

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283