如何停止跟踪 Git 中的远程分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:34:43  来源:igfitidea点击:

How do you stop tracking a remote branch in Git?

gitbranchgit-track

提问by Jason Cohen

How do you stop tracking a remote branch in Git?

如何停止跟踪Git 中的远程分支?

I am asking to stop tracking because in my concrete case, I want to delete the local branch, but not the remote one. Deleting the local one and pushing the deletion to remote will delete the remote branch as well:

我要求停止跟踪,因为在我的具体情况下,我想删除本地分支,而不是远程分支。删除本地分支并将删除推送到远程也会删除远程分支:

Can I just do git branch -d the_branch, and it won't get propagated when I later git push?

我可以这样做吗git branch -d the_branch,以后我就不会传播了git push

Will it only propagate if I were to run git push origin :the_branchlater on?

它只会在我git push origin :the_branch稍后运行时传播吗?

回答by VonC

As mentioned in Yoshua Wuyts' answer, using git branch:

正如Yoshua Wuyts回答中提到的,使用git branch

git branch --unset-upstream

Other options:

其他选项:

You don't have to delete your local branch.

您不必删除本地分支。

Simply delete the local branch that is tracking the remote branch:

只需删除跟踪远程分支的本地分支:

git branch -d -r origin/<remote branch name>

-r, --remotestells git to delete the remote-tracking branch (i.e., delete the branch set to track the remote branch). This will notdelete the branch on the remote repo!

-r, --remotes告诉 git 删除远程跟踪分支(即删除设置跟踪远程分支的分支)。这不会删除该分支上的远程回购

See "Having a hard time understanding git-fetch"

参见“很难理解 git-fetch

there's no such concept of local tracking branches, only remote tracking branches.
So origin/masteris a remote tracking branch for masterin the originrepo

没有本地跟踪分支的概念,只有远程跟踪分支。
所以origin/master是一个远程跟踪分行masterorigin回购

As mentioned in Dobes Vandermeer's answer, you also need to reset the configuration associated to the localbranch:

正如Dobes Vandermeer回答中提到的,您还需要重置与本地分支关联的配置:

git config --unset branch.<branch>.remote
git config --unset branch.<branch>.merge

Remove the upstream information for <branchname>.
If no branch is specified it defaults to the current branch.

删除 的上游信息<branchname>
如果未指定分支,则默认为当前分支。

(git 1.8+, Oct. 2012, commit b84869eby Carlos Martín Nieto (carlosmn))

(git 1.8+,2012 年 10 月,由Carlos Martín Nieto ( )提交 b84869ecarlosmn

That will make any push/pull completely unaware of origin/<remote branch name>.

这将使任何推/拉完全不知道origin/<remote branch name>.

回答by Yoshua Wuyts

To remove the upstream for the current branch do:

要删除当前分支的上游,请执行以下操作:

$ git branch --unset-upstream

This is available for Git v.1.8.0 or newer. (Sources: 1.7.9 ref, 1.8.0 ref)

这适用于 Git v.1.8.0 或更新版本。(来源:1.7.9 参考1.8.0 参考

source

来源

回答by Dobes Vandermeer

To remove the association between the local and remote branch run:

要删除本地和远程分支之间的关联,请运行:

git config --unset branch.<local-branch-name>.remote
git config --unset branch.<local-branch-name>.merge

Optionally delete the local branch afterwards if you don't need it:

如果您不需要它,可以选择在之后删除本地分支:

git branch -d <branch>

This won't delete the remote branch.

这不会删除远程分支。

回答by Jacob Groundwater

The simplest way is to edit .git/config

最简单的方法是编辑 .git/config

Here is an example file

这是一个示例文件

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
[remote "origin"]
        url = [email protected]:repo-name
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "test1"]
        remote = origin
        merge = refs/heads/test1
[branch "master"]
        remote = origin
        merge = refs/heads/master

Delete the line merge = refs/heads/test1in the test1branch section

删除线merge = refs/heads/test1test1分支部分

回答by CletusW

You can delete the remote-tracking branch using

您可以使用删除远程跟踪分支

git branch -d -r origin/<remote branch name>

as VonC mentions above. However, if you keep your local copy of the branch, git pushwill still try to push that branch(which could give you a non-fast-forward error as it did for ruffin). This is because the config push.defaultdefaults to matchingwhich means:

正如 VonC 上面提到的。但是,如果您保留分支的本地副本,git push仍会尝试推送该分支(这可能会给您带来非快进错误,就像 ruffin 所做的那样)。这是因为配置push.default默认为matching这意味着:

matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.

匹配 - 推送所有匹配的分支。两端具有相同名称的所有分支都被认为是匹配的。这是默认设置。

(see http://git-scm.com/docs/git-configunder push.default)

(见http://git-scm.com/docs/git-config下面push.default

Seeing as this is probably not what you wanted when you deleted the remote-tracking branch, you can set push.defaultto upstream(or trackingif you have git < 1.7.4.3)

当您删除远程跟踪分支时,这可能不是您想要的,您可以设置push.defaultupstream(或者tracking如果您有 git < 1.7.4.3)

upstream - push the current branch to its upstream branch.

上游 - 将当前分支推送到其上游分支。

using

使用

git config push.default upstream

and git will stop trying to push branches that you have "stopped tracking."

并且 git 将停止尝试推送您“停止跟踪”的分支。

Note:The simpler solution would be to just rename your local branch to something else. That would eliminate some potential for confusion, as well.

注意:更简单的解决方案是将本地分支重命名为其他名称。这也将消除一些混淆的可能性。

回答by infomaniac

Here's a one-liner to remove all remote-tracking branches matching a pattern:

这是一个单行删除匹配模式的所有远程跟踪分支:

git branch -rd $(git branch -a | grep '{pattern}' | cut -d'/' -f2-10 | xargs)

git branch -rd $(git branch -a | grep '{pattern}' | cut -d'/' -f2-10 | xargs)

回答by qneill

This is not an answer to the question, but I couldn't figure out how to get decent code formatting in a comment above... so auto-down-reputation-be-damned here's my comment.

这不是问题的答案,但我无法弄清楚如何在上面的评论中获得体面的代码格式......所以这里是我的评论。

I have the recipe submtted by @Dobes in a fancy shmancy [alias] entry in my .gitconfig:

我在我的 .gitconfig 中的一个花哨的 shmancy [别名] 条目中有 @Dobes 提交的配方:

# to untrack a local branch when I can't remember 'git config --unset'
cbr = "!f(){ git symbolic-ref -q HEAD 2>/dev/null | sed -e 's|refs/heads/||'; }; f"
bruntrack = "!f(){ br=${1:-`git cbr`};  \
    rm=`git config --get branch.$br.remote`; \
    tr=`git config --get branch.$br.merge`; \
    [ $rm:$tr = : ] && echo \"# untrack: not a tracking branch: $br\" && return 1; \
    git config --unset branch.$br.remote; git config --unset branch.$br.merge; \
    echo \"# untrack: branch $br no longer tracking $rm:$tr\"; return 0; }; f"

Then I can just run

那我就可以跑了

$ git bruntrack branchname

回答by Bob

git branch --unset-upstreamstops tracking all the local branches, which is not desirable.

git branch --unset-upstream停止跟踪所有本地分支,这是不可取的。

Remove the [branch "branch-name"]section from the .git/configfile followed by

[branch "branch-name"].git/config文件中删除该部分,然后是

git branch -D 'branch-name' && git branch -D -r 'origin/branch-name'

git branch -D 'branch-name' && git branch -D -r 'origin/branch-name'

works out the best for me.

最适合我。

回答by H?ng

You can use this way to remove your remote branch

您可以使用这种方式删除远程分支

git remote remove <your remote branch name>

回答by Mark Caudill

The easiest way to do this is to delete the branch remotely and then use:

最简单的方法是远程删除分支,然后使用:

git fetch --prune (aka git fetch -p)

git fetch --prune(又名 git fetch -p)