使用repo abandon和git branch -D删除分支

mru*_*jay 5 git git-repo

我正在为我的项目使用git和git-repo.我看到当我尝试使用git命令删除我当前使用的本地分支时

git branch -D branch_name
Run Code Online (Sandbox Code Playgroud)

它显示了我期待的错误,因为我们无法删除当前分支.

但如果我使用repo命令

repo abandon branch_name
Run Code Online (Sandbox Code Playgroud)

我能够删除当前分支.所以我的问题是什么命令是repo使用内部删除分支?

Von*_*onC 4

subcmd调用project.AbandonBranch,其中包括abandon.py

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)
Run Code Online (Sandbox Code Playgroud)

换句话说,它确保不在您要删除的分支上,即使这意味着设置一个分离的 HEAD(通过检查 SHA1 ' revid')。