简体   繁体   English

删除pygit2中的引用

[英]delete a reference in pygit2

I am using the library pygit2 to implement a git module on my project. 我正在使用库pygit2在我的项目上实现git模块。 Right now i'm blocked by these 2 scenarios : - the deletion of a tag than a push - the deletion of a branch than a push 现在,我被这两种情况所阻止:-删除标签而不是推送-删除分支而不是推送

this is what i'm trying to do : 这就是我想要做的:

>>> branch = repo.lookup_branch('origin/1249something', pygit2.GIT_BRANCH_REMOTE)
>>> branch
<_pygit2.Branch object at 0xb7350270>
>>> branch.delete()
>>> branch_to_push = 'refs/remotes/origin/1249something'
>>> pusher = pygit2.Signature('test TESTSSSSSSSS', 'test-test1@site.com')
>>> message = "just test message"
>>> remote_repo = repo.remotes[0]
>>> remote_repo.push(branch_to_push, pusher, message)

and this is the result i get : 这是我得到的结果:

File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pygit2/remote.py", line 358, in push
    check_error(err)
  File "/usr/local/lib/python2.7/dist-packages/pygit2/errors.py", line 56, in check_error
    raise GitError(message)
_pygit2.GitError: src refspec 'refs/remotes/origin/1249something' does not match any existing object

The name refs/remotes/origin/1249something is not the branch's name, but your local idea of what the remote's branch looks like. 名称refs/remotes/origin/1249something不是分支的名称,而是您对远程分支的外观的本地想法。 Assuming a default fetch refspecs (ie you did git remote add origin <url> ) the branch on the remote is called refs/heads/1249something . 假设默认获取refspecs(即您执行了git remote add origin <url> ),则远程分支称为refs/heads/1249something When you look up origin/1249something , that's not a branch on the remote, but a remote-tracking branch, it serves as a mirror to the state of that branch the last time you ran a fetch. 当您查找origin/1249something ,它不是远程服务器上的分支,而是远程跟踪分支,当您上一次运行访origin/1249something ,它充当该分支状态的镜像。

Deleting your remote-tracking branch locally is not going to affect the remote repository in any way, and the next time you perform a fetch, it's going to show up again. 在本地删除远程跟踪分支不会以任何方式影响远程存储库,下次您执行访存操作时,它将再次显示。

If you'd like to remove the branch from the remote, you'll need to specify a refspec which says this. 如果要从远程删除分支,则需要指定一个refspec来说明。 This is done by providing en empty local part, in your case, you'd do it by providing :refs/heads/1249something as the refspec to push. 这是通过提供一个空的本地部分来完成的,在您的情况下,您可以通过提供:refs/heads/1249something作为要推送的:refs/heads/1249something来实现。 The empty part left of the colon means that you want the reference on the right to be deleted. 冒号左边的空白部分表示您要删除右边的引用。

remote = repo.remotes[0]
remote.push(':refs/heads/1249something', pusher, message)

will delete that branch from the remote. 将从远程删除该分支。

I would suggest reading Working with Remotes to cement your understanding of how remotes and remote-tracking branches relate to the remote repository. 我建议阅读“与远程服务器一起使用”,以加深您对远程服务器和远程跟踪分支与远程存储库的关系的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM