如何停止跟踪 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
How do you stop tracking a remote branch in Git?
提问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_branch
later 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, --remotes
tells 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.
Soorigin/master
is a remote tracking branch formaster
in theorigin
repo
没有本地跟踪分支的概念,只有远程跟踪分支。
所以origin/master
是一个远程跟踪分行master
的origin
回购
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 ( )提交 b84869e)carlosmn
That will make any push/pull completely unaware of origin/<remote branch name>
.
这将使任何推/拉完全不知道origin/<remote branch name>
.
回答by Yoshua Wuyts
回答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/test1
in the test1
branch section
删除线merge = refs/heads/test1
的test1
分支部分
回答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 push
will 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.default
defaults to matching
which 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.default
to upstream
(or tracking
if you have git < 1.7.4.3)
当您删除远程跟踪分支时,这可能不是您想要的,您可以设置push.default
为upstream
(或者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-upstream
stops tracking all the local branches, which is not desirable.
git branch --unset-upstream
停止跟踪所有本地分支,这是不可取的。
Remove the [branch "branch-name"]
section from the .git/config
file 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)