问题
How do you stop tracking a remote branch in 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:
- How do I delete a Git branch both locally and in GitHub?
Can I just do git branch -d the_branch
, and it won\'t get propagated when I later git push
?
Will it only propagate if I were to run git push origin :the_branch
later on?
回答1:
As mentioned in Yoshua Wuyts' answer, using 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 not delete the branch on the remote repo!
See "Having a hard time understanding 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
As mentioned in Dobes Vandermeer's answer, you also need to reset the configuration associated to the local branch:
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.
(git 1.8+, Oct. 2012, commit b84869e by Carlos Martín Nieto (carlosmn))
That will make any push/pull completely unaware of origin/<remote branch name>
.
回答2:
To remove the upstream for the current branch do:
$ git branch --unset-upstream
This is available for Git v.1.8.0 or newer. (Sources: 1.7.9 ref, 1.8.0 ref)
source
回答3:
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.
回答4:
The simplest way is to edit .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
回答5:
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:
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
)
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)
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."
Note: The simpler solution would be to just rename your local branch to something else. That would eliminate some potential for confusion, as well.
回答6:
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)
回答7:
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:
# 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
回答8:
git branch --unset-upstream
stops tracking all the local branches, which is not desirable.
Remove the [branch "branch-name"]
section from the .git/config
file followed by
git branch -D 'branch-name' && git branch -D -r 'origin/branch-name'
works out the best for me.
回答9:
You can use this way to remove your remote branch
git remote remove <your remote branch name>
回答10:
The easiest way to do this is to delete the branch remotely and then use:
git fetch --prune (aka git fetch -p)
来源:https://stackoverflow.com/questions/3046436/how-do-you-stop-tracking-a-remote-branch-in-git