简体   繁体   English

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

[英]How do you stop tracking a remote branch in Git?

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传播吗?

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 (ie, delete the branch set to track the remote branch). -r, --remotes告诉git删除远程跟踪分支(即删除分支集以跟踪远程分支)。 This will not delete the branch on the remote repo ! 不会删除远程仓库上的分支

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/master is a remote tracking branch for master in the origin repo 所以origin/master是一个远程跟踪分支masterorigin回购

As mentioned in Dobes Vandermeer 's answer , you also need to reset the configuration associated to the local branch: 正如Dobes Vandermeer回答中所提到的,您还需要重置与本地分支关联的配置:

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

Remove the upstream information for <branchname> . 删除<branchname>的上游信息。
If no branch is specified it defaults to the current branch. 如果未指定分支,则默认为当前分支。

(git 1.8+, Oct. 2012, commit b84869e by Carlos Martín Nieto ( carlosmn ) ) (git 1.8 +,2012年10月,由CarlosMartínNieto 承诺b84869e carlosmn

That will make any push/pull completely unaware of origin/<remote branch name> . 这将使任何推/拉完全不知道origin/<remote branch name>

To remove the upstream for the current branch do: 要删除当前分支的上游,请执行以下操作:

$ git branch --unset-upstream

This is available for Git v.1.8.0 or newer. 这适用于Git v.1.8.0或更高版本。 (Sources: 1.7.9 ref , 1.8.0 ref ) (资料来源: 1.7.9 ref1.8.0 ref

source 资源

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. 这不会删除远程分支。

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 = git@example.com: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 删除test1分支部分中的merge = refs/heads/test1

You can delete the remote-tracking branch using 您可以使用删除远程跟踪分支

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

as VonC mentions above. 正如VonC上面提到的那样。 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 ). 但是,如果保留分支的本地副本, git push 仍会尝试推送该分支 (这可能会给你一个非快进错误, 就像它为ruffin做的那样 )。 This is because the config push.default defaults to matching which means: 这是因为config 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-config under push.default ) (见http://git-scm.com/docs/git-configpush.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 (或者如果您有git <1.7.4.3则进行tracking

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. 这也将消除一些混淆的可能性。

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

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

Local-tracking branches本地跟踪分支

If you're talking about local branches (eg main , dev ) that are configured to push -to and pull -from an upstream [remote branch], then you can disable that with:如果您正在谈论配置为从上游 [远程分支] pushpull本地分支(例如maindev ),那么您可以禁用它:

❯ git branch --unset-upstream <LOCALBRANCH>

Eg:例如:

❯ git branch --unset-upstream dev
❯ git branch --unset-upstream feature-x

Remote-tracking branches远程跟踪分支

If you're talking about branches of the name <REMOTE>/<BRANCH> (eg origin/main , origin/dev ) that show up in your git log (and .git/refs/remotes/<REMOTE>/ directory) showing you the state of a remote branch, then you can stop having it "tracked" (having it updated) by overwriting the current list of held remote-tracking branches with your own new custom list:如果您谈论的是名称<REMOTE>/<BRANCH> (例如origin/mainorigin/dev )的分支,这些分支出现在您的git log (和.git/refs/remotes/<REMOTE>/目录)中,显示您了解远程分支的状态,然后您可以通过用您自己的新自定义列表覆盖当前持有的远程跟踪分支列表来停止“跟踪”(更新)它:

❯ git remote set-branches <REMOTE> [<REMOTE-BRANCH> …]

If additionally, you don't want to see those remote-tracking branches anymore in your git log (and .git/refs/remotes/<REMOTE>/ directory), then you can remove them with:此外,如果您不想再在git log (和.git/refs/remotes/<REMOTE>/目录)中看到那些远程跟踪分支,那么您可以使用以下命令删除它们:

❯ git branch --delete --remotes -- <REMOTE>/<BRANCH>
Deleted remote-tracking branch <REMOTE>/<BRANCH> (was 1f1a655).

Eg:例如:

# keep tracking `origin/main`, and `origin/dev`,
# untrack all other `origin/*` remote branches
❯ git remote set-branches origin main dev

# delete remote branches previously tracked, from the
# `.git/refs/remotes/<REMOTE>/` directory
❯ git branch --delete --remotes -- origin/feature-x origin/feature-y
❯ git branch --delete --remotes -- origin/hotfix-z

Stale Remote branches陈旧的远程分支

Finally, if there are remote branches that have been removed from the remote repository itself (have become stale), and you want to update your local repository to reflect that, then you can by deleting (pruning) them:最后,如果有远程分支已从远程存储库本身中删除(已变得陈旧),并且您想更新本地存储库以反映这一点,那么您可以删除(修剪)它们:

# automatically
❯ git remote prune <REMOTE>
Pruning <REMOTE>
URL: <REMOTEURL>
 * [pruned] <REMOTE>/<BRANCH>

...or ...或者

# manually
❯ git branch --delete --remotes -- <REMOTE>/<BRANCH>
Deleted remote-tracking branch <REMOTE>/<BRANCH> (was 1f1a655).

PS聚苯乙烯

You can check the state of tracking with:您可以通过以下方式检查跟踪状态:

❯ git remote show <REMOTE>

Eg:例如:

❯ git remote show origin
* remote origin
  Fetch URL: /Users/johndoe/bare-remote
  Push  URL: /Users/johndoe/bare-remote
  HEAD branch: ant
  Remote branches:
    brooms  tracked
    bull    tracked
    cat     tracked
    deer    tracked
    dog     tracked
    foxy    tracked
    john    tracked
    master  tracked
    new     tracked
    tim     tracked
    timothy tracked
  Local branches configured for 'git pull':
    ant    merges with remote ant
    master merges with remote master
  Local refs configured for 'git push':
    ant    pushes to ant    (up to date)
    master pushes to master (up to date)

  • git-remote(1): git-remote(1):

    set-branches : Changes the list of branches tracked by the named remote. set-branches :更改指定远程跟踪的分支列表。 This can be used to track a subset of the available remote branches after the initial setup for a remote.这可用于在远程初始设置后跟踪可用远程分支的子集。

    prune : Deletes stale references associated with . prune :删除与 . By default, stale remote-tracking branches under are deleted, but depending on global configuration and the configuration of the remote we might even prune local tags that haven't been pushed there.默认情况下,删除过时的远程跟踪分支,但根据全局配置和远程配置,我们甚至可能会修剪尚未推送到那里的本地标签。

    show : Gives some information about the remote . show :提供有关遥控器的一些信息。

  • git-branch(1): git 分支(1):

    --unset-upstream : Remove the upstream information for . --unset-upstream :删除上游信息。

    --delete : Delete a branch. --delete : 删除一个分支。

    --remotes : List or delete (if used with -d) the remote-tracking branches. --remotes :列出或删除(如果与 -d 一起使用)远程跟踪分支。

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 [alias]条目中有@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

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 .git/config文件中删除[branch "branch-name"]部分,然后删除

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

works out the best for me. 对我来说是最好的。

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

git remote remove <your remote branch name>

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)

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

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