如何停止跟踪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 config --unset branch.<local-branch-name>.remote
git config --unset branch.<local-branch-name>.merge
如果你不需要,可以选择删除本地分支:
git branch -d <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.
正如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/<远程分支名称>。
删除本地分支和远程分支之间的关联:
git config --unset branch.<local-branch-name>.remote
git config --unset branch.<local-branch-name>.merge
如果你不需要,可以选择删除本地分支:
git branch -d <branch>
这不会删除远程分支。
这不是问题的答案,但我不知道如何在上面的评论中获得体面的代码格式…因此,我的评论是自动降低声誉。
在我的.gitconfig中,我有@Dobes在一个花哨的shmancy[别名]条目中提交的食谱:
# 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"
然后我就可以跑了
$ git bruntrack branchname
最简单的方法是编辑.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行