如何停止跟踪Git中的远程分支?
我要求停止跟踪,因为在我的具体情况下,我想删除本地分支,但不是远程分支。删除本地分支并将删除推到remote也会删除远程分支:
如何在本地和GitHub中删除Git分支?
我能不能只做git branch -d the_branch,当我之后推送git时,它不会被传播?
如果我稍后运行git push origin:the_branch,它是否只会传播?
如何停止跟踪Git中的远程分支?
我要求停止跟踪,因为在我的具体情况下,我想删除本地分支,但不是远程分支。删除本地分支并将删除推到remote也会删除远程分支:
如何在本地和GitHub中删除Git分支?
我能不能只做git branch -d the_branch,当我之后推送git时,它不会被传播?
如果我稍后运行git push origin:the_branch,它是否只会传播?
当前回答
要移除当前分支的上游,请执行以下操作:
$ git branch --unset-upstream
这可用于Git v.1.8.0或更新版本。(来源:1.7.9 ref, 1.8.0 ref)
源
其他回答
下面是删除所有与模式匹配的远程跟踪分支的一行代码:
Git branch -rd $(Git branch -a | grep '{pattern}' | cut -d'/' -f2-10 | xargs)
最简单的方法是编辑.git/config
下面是一个示例文件
[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
删除test1分支节中的merge = refs/heads/test1行
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)
源
您可以使用这种方式删除远程分支
git remote remove <your remote branch name>