本文介绍了在git中删除远程分支:一劳永逸地理解它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的为此疯狂。坦率地说,我不知道为什么我很难理解这一点,因为它应该很简单,对吧?首先我已经完成了大量的搜索。我已经阅读了一堆关于stackoverflow的东西,我已经阅读了很多谷歌搜索发现的博客文章。尽管如此,有些细节仍然不清楚,尽管这个问题看似重复且简单,所以我向那些在downvote / close按钮上悬停的人提前道歉。我今天在这里发帖的原因是因为我在命令行上看到的行为与我实际在线阅读的行为不同。所以它让我困惑。



这里的两个命令是:

  git branch -r -d origin / topic1 

  git push origin:topic1 

我的(我可能完全错误,但我的大脑被炸):


  • 第一个应该删除跟踪参考远程分支

  • 第二个应该删除远程分支但不是跟踪引用(如果存在本地分支),尽管您不会删除跟踪引用似乎毫无意义并且让我对初学者感到困惑。 下面是我遇到的一个最近的场景。我只是克隆了一个有两个远程分支的仓库:

      origin / master 
    origin / develop

    code>

    我拥有的唯一本地分支是:

      master 

    我想删除远程分支 origin /开发,所以看看这个:

      Robert @ COMP / c / Code / project(master)
    $ git branch -rd origin / develop
    删除了远程分支的起源/开发(是9ff16e8)。

    Robert @ COMP / c / Code / project(master)
    $ git fetch
    从github.com:username/project
    * [new branch] develop - >原产地/开发

    正如您所看到的,我试图删除远程分支,并立即获取最新从原产地变化,但由于某种原因,它重新创建分支。我不知道为什么它会这样做,我很困惑。我没有为 origin / develop 的本地对应分支,所以我不知道它为什么这样做。


    $ b $我想知道为什么发生这种情况,但也(为了解决这个问题的更一般的标题,或许可以帮助其他人清除堆栈溢出以找出这些令人困惑的含糊不清的问题的答案),我想要更通用一些回答一些问题:


    • 有两种方法可以删除远程分支。什么是真正的好,简单的方法来记住在哪种情况下使用哪一种?

    • 假设它很重要,相应的本地分支(对于远程分支)影响如何删除远程分支的决定?

    • 本地和远程分支之间的跟踪引用/关系的存在如何影响您选择删除分支的方式,以及删除分支的方法也会清除跟踪引用?



    谢谢大家!请保存我的头发!

    编辑(答案摘要)



    根据以下答案,我是能够澄清一件令人迷惑的事情。 git branch -r 选项的文档声明:

    对我来说,困惑是被认为是遥远的。对我而言,最初的意思是起源......我的情况是github。但是,在你的本地克隆中,github有自己的远程分支的概念,这是它自己的远程分支的副本。所以基本上看起来,删除远程分支涉及两个步骤:删除原始分支(这是 git push origin:topic1 ),然后删除远程跟踪分支( git branch -rb origin / topic1 )。

    这有助于我的理解,尽管因为单词remote有两个这意味着,这让人非常困惑,所以我希望这个修改对我的问题有助于未来的人们。感谢所有回答问题的人以及未来将继续为此问题提供帮助的人!

    第一个应该删除跟踪引用和远程分支


    否:它是本地操作,所以它只能删除远程跟踪分支(位于您的仓库中)



    远程跟踪分支在此记住从远程仓库分支中获取的最后一个SHA1。

    如果你想删除那个分支(在远程仓库),你需要告诉远程仓库(因此 git push:topic1



    本地分支(您的仓库中的一个分支)可以是:


    • 一个简单的分支

    • 远程追踪分支(由获取创建的分支)

    • 本地追踪分支(因为它有一个 与之相关联)



    正如,一旦你删除了远程仓库的几个分支,你可以修剪你所在地区仓库的所有远程追踪分支:

      git remote prune origin 


    I'm really going crazy over this. Quite frankly I'm not sure why I'm having trouble understanding this as it should be simple, right? First of all I've done tons of searching on this. I've read a bunch of stuff on stackoverflow, I've read blog articles found through lots of Google searching. However some details are still unclear, despite however duplicated and simple this question may seem, so my apologies in advance to the people that are hovering over the downvote/close buttons. The reason why I'm posting here today is because the behavior I see on the command line differs from what I actually read online. So it confuses me.

    The two commands under question here are:

    git branch -r -d origin/topic1
    

    and

    git push origin :topic1
    

    My understanding so far (I may be completely wrong though, my brain is fried):

    • The first should remove both the tracking reference and the remote branch
    • The second should remove the remote branch but NOT the tracking reference (if a local branch exists), although the reason why you wouldn't remove the tracking reference seems pointless and confusing to me as a sort-of-beginner.

    Here is a recent scenario I have run into. I just cloned a repository with two remote branches:

    origin/master
    origin/develop
    

    The only local branch I have is:

    master
    

    I want to delete the remote branch origin/develop, so watch this:

    Robert@COMP /c/Code/project (master)
    $ git branch -rd origin/develop
    Deleted remote branch origin/develop (was 9ff16e8).
    
    Robert@COMP /c/Code/project (master)
    $ git fetch
    From github.com:username/project
     * [new branch]      develop    -> origin/develop
    

    As you can see, I tried to delete the remote branch, and immediately fetch the latest changes from origin, but for some reason it recreated the branch. I have no idea why it would do this, I'm very confused. I don't have a local corresponding branch for origin/develop, so I don't know why it is doing this.

    I'd like to know why this happened, but also (to address the more general title of this question, and to perhaps help everyone else scavenging stack overflow for answers to these confusing ambiguities), I'd like more general answers to some questions:

    • There are two ways to delete a remote branch. What is a really good, simple way to remember which one to use under which circumstance?
    • Assuming it matters at all, how does the existence of a corresponding local branch (for a remote branch) affect the decision of how a remote branch is deleted?
    • How does the existence of a tracking reference/relationship between a local and remote branch affect how you choose to delete a branch, and which of the methods of deleting a branch also cleans up tracking references?

    Thanks everyone! Please, save my hair!

    EDIT (summary of answers)

    Based on the answers below, I was able to get clarification on the one piece that was confusing everything about this. The documentation for the -r option for git branch states:

    The confusion for me was with what is considered "Remote". Remote to me, initially, meant "origin"... what was on "github" in my case. However, in your LOCAL clone, github has its own concept of what a remote is, and that's its own copy of the remote branches. So basically it seems that deleting a remote branch involves two steps: Deleting the origin's branch (this is the git push origin :topic1) and then deleting the remote tracking branch (the git branch -rb origin/topic1).

    That helps my understanding, although because the word "remote" has two meanings here, it makes it VERY confusing to communicate this to people, so I hope this amendment to my question helps people in the future. Thanks to everyone who answered and who will continue to contribute to this question in the future!

    解决方案

    No: it is a local operation, so it can only delete the remote tracking branch (which is within your repo)

    The remote tracking branch is here to remember the last SHA1 fetched from the branch on the remote repo.
    If you want to delete that branch (on the remote repo), you need to tell that remote repo (hence the git push :topic1)

    A local branch (one within your repo) can be:

    • a simple branch
    • a remote tracking branch (one created by a fetch)
    • a local tracking branch (because it has an upstream branch associated to it)

    As mentioned in "How can I delete all git branches which are already merged?", once you have delete several branches of a remote repo, you can prune all the remote tracking branches of your local repo with a:

    git remote prune origin
    

    这篇关于在git中删除远程分支:一劳永逸地理解它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 13:23