Deleting branch with repo abandon and git branch -D
Asked Answered
S

1

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?

Schonthal answered 23/5, 2013 at 5:13 Comment(1)
If anyone is interested in doing research on this, you can find the source code for the abandon command at gerrit.googlesource.com/git-repo/+/master/subcmds/abandon.pyWealth
R
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').

Racer answered 23/5, 2013 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.