Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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:

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?

share|improve this question
up vote 345 down vote accepted

You don't have to delete your local branch.

Simply delete your remote tracking branch:

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

(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.
So origin/master is a remote tracking branch for master in the origin 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

Or, as mentioned in Yoshua Wuyts' answer, still using git branch:

git branch --unset-upstream

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>.

share|improve this answer
6  
The remote tracking branch is recreated after git fetch. Is it possible to exclude these? – Matt R Aug 6 '10 at 10:46
1  
@Matt: I believe this would be done by setting the remote.<name>.fetch refspec config setting, but I am not sure if you can exclude a branch when specifying a refspec. – VonC Aug 6 '10 at 11:04
    
It appeared that way to me too; I was able to rebase my local branch though and after the fast-forward the apparent connection between the branches disappeared. – willoller Aug 30 '10 at 21:18
5  
@ruffin: this is completely normal: git branch -d -r origin/<remote branch name> will delete a remote tracking branch as declared locally, in your repo. It will not delete the branch on the remote repo itself (only a git push :development would do that). So when you are pushing your local development, with an history different than the remote development branch (on the remote repo), you will still get the non-fast-forward warning. – VonC May 15 '12 at 18:06
1  
@Marco true, but I prefer doing it anyway, I find it "cleaner". – VonC Mar 11 '13 at 15:29

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.

share|improve this answer
1  
worked great! Thanks! – Jason T Featheringham Dec 17 '12 at 22:59
10  
git branch -d <branch> is not required to remove the association. – Marco Mar 11 '13 at 15:27
1  
Im using JIRA. When I do pull again the branch appears. How can I prevent that? – powder366 Jan 13 '15 at 13:42
1  
@Marco Correct, but if you use git branch -vv you will still see the old branches until you do git branch -d <branch>. – SeldomNeedy Feb 13 at 8:01
1  
@SeldomNeedy I think you might be missing the point of a DVCS? The point is that I can have a branch that starts out tracking an upstream branch, but that I detach in order to pursue an approach that the upstream branch is not using. In that case, the branch is neither "old" (because I'm still developing on it) nor "disused" (because I'm using it). This is a large part of the design of a DVCS.. Does that make sense? – Marco Feb 16 at 14:00

To remove the upstream for the current branch do:

$ git branch --unset-upstream

source

share|improve this answer
8  
Should have been the chosen answer. – kaiser Jun 12 '15 at 14:07
    
If you don't want to remove the current branch, simply: git branch --unset-upstream [branchname] – Sonata Oct 19 at 8:36

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

share|improve this answer
2  
+1 I cannot say definitively that this is the easiest method, but it certainly proves the easiest for me to understand (and hopefully therefor to remember)! – sage Jan 9 '13 at 20:07
    
Nice solution +1. This is actually the place where the tracking is registered, easy to change, clear what it does. Commandline .git/config is available, too. – hakre Apr 24 '13 at 13:43
4  
fyi, this is the same as the "git config --unset" solution by Dobes Vandermeer. "git config" edits this file. – J.Z. Jul 28 '13 at 16:03

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.

share|improve this answer

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)

share|improve this answer

The easiest way to do this is to delete the branch remotely and then use:

git fetch --prune (aka git fetch -p)

share|improve this answer
8  
I'm confused, the OP did not wanted to delete the remote branch. – madth3 Oct 2 '13 at 23:46

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
share|improve this answer
    
+1. Nice alias, in addition to my answer above. – VonC Jun 4 '14 at 17:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.