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

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

如何在本地和GitHub中删除Git分支?

我能不能只做git branch -d the_branch,当我之后推送git时,它不会被传播?

如果我稍后运行git push origin:the_branch,它是否只会传播?


当前回答

Local-tracking分支

如果你正在谈论的本地分支(例如main, dev)被配置为从上游[远程分支]推入和拉出,那么你可以禁用它:

❯ git branch --unset-upstream <LOCALBRANCH>

例如:

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

Remote-tracking分支

如果你谈论的是名称为<REMOTE>/<BRANCH>的分支(例如origin/main, origin/dev),这些分支出现在你的git日志中(和 .git/refs/remotes/<REMOTE>/ directory)显示远程分支的状态,然后你可以用你自己的新自定义列表覆盖当前持有的远程跟踪分支列表来停止“跟踪”(更新它):

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

此外,如果你不想在你的git日志(和.git/refs/remotes/<REMOTE>/目录)中看到这些远程跟踪分支,那么你可以删除它们:

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

例如:

# 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

陈旧的远程分支

最后,如果有远程分支已经从远程存储库本身删除(已经过时),并且你想要更新本地存储库来反映这一点,那么你可以通过删除(修剪)它们:

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

,或者

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

PS

您可以通过以下命令查看跟踪状态:

❯ git remote show <REMOTE>

例如:

❯ 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): set-branches: Changes the list of branches tracked by the named remote. 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 . 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 . git-branch(1): --unset-upstream: Remove the upstream information for . --delete: Delete a branch. --remotes: List or delete (if used with -d) the remote-tracking branches.

其他回答

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

$ git branch --unset-upstream

这可用于Git v.1.8.0或更新版本。(来源:1.7.9 ref, 1.8.0 ref)

正如Yoshua Wuyts的回答中提到的,使用git分支:

git branch --unset-upstream

其他选项:

您不必删除本地分支。

简单地删除跟踪远程分支的本地分支:

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

-r,——remotes告诉git删除远程跟踪分支(即删除跟踪远程分支的分支集)。这将不会删除远程回购上的分支!

参见“很难理解git-fetch”

没有本地跟踪分支的概念,只有远程跟踪分支。 因此,origin/master是在origin repo中对master的远程跟踪分支

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

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

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

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

这将使任何推/拉完全不知道origin/<远程分支名称>。

Local-tracking分支

如果你正在谈论的本地分支(例如main, dev)被配置为从上游[远程分支]推入和拉出,那么你可以禁用它:

❯ git branch --unset-upstream <LOCALBRANCH>

例如:

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

Remote-tracking分支

如果你谈论的是名称为<REMOTE>/<BRANCH>的分支(例如origin/main, origin/dev),这些分支出现在你的git日志中(和 .git/refs/remotes/<REMOTE>/ directory)显示远程分支的状态,然后你可以用你自己的新自定义列表覆盖当前持有的远程跟踪分支列表来停止“跟踪”(更新它):

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

此外,如果你不想在你的git日志(和.git/refs/remotes/<REMOTE>/目录)中看到这些远程跟踪分支,那么你可以删除它们:

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

例如:

# 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

陈旧的远程分支

最后,如果有远程分支已经从远程存储库本身删除(已经过时),并且你想要更新本地存储库来反映这一点,那么你可以通过删除(修剪)它们:

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

,或者

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

PS

您可以通过以下命令查看跟踪状态:

❯ git remote show <REMOTE>

例如:

❯ 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): set-branches: Changes the list of branches tracked by the named remote. 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 . 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 . git-branch(1): --unset-upstream: Remove the upstream information for . --delete: Delete a branch. --remotes: List or delete (if used with -d) the remote-tracking branches.

下面是删除所有与模式匹配的远程跟踪分支的一行代码:

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

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

从.git/config文件中删除[branch "branch-name"]段

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

这对我来说是最好的。