774

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?

Community
  • 1
  • 1
Jason Cohen
  • 81,399
  • 26
  • 107
  • 114

11 Answers11

1075

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

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

MTV
  • 91
  • 9
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 9
    The remote tracking branch is recreated after git fetch. Is it possible to exclude these? – Matt R Aug 06 '10 at 10:46
  • 1
    @Matt: I believe this would be done by setting the `remote..fetch` refspec config setting, but I am not sure if you can exclude a branch when specifying a refspec. – VonC Aug 06 '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
  • Didn't work for me. When pushing, I'm still getting warnings about the branches I deleted. As the OP, I want to stop tracking some of the remote branches I pushed to in the past. So after `git branch -d -r origin/development`, when I `git push` I still get `! [rejected] development -> development (non-fast-forward)` – ruffin May 15 '12 at 15:59
  • 8
    @ruffin: this is completely normal: `git branch -d -r origin/` 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
  • @VonC I did this primarily to have this repository "forget" it was tracking branch no longer used (in this clone), and this does unfortunately not help. – Thorbjørn Ravn Andersen Dec 10 '12 at 12:50
  • @ThorbjørnRavnAndersen what `git branch -a` and `git config -l` return (in particular `branch.name.remote` and `branch.name.merge`)? Those information could be the basis of a *new* Stack Overflow question. – VonC Dec 10 '12 at 13:10
  • I found that the steps provided by @Dobes Vandermeer worked - the repository forgot about the branch I once visited in this clone, but did not want to work on again there. – Thorbjørn Ravn Andersen Dec 10 '12 at 14:18
  • @VonC: *deleting* the remote branch is **not** required to stop tracking it! – Marco Mar 11 '13 at 15:27
  • 1
    @Marco true, but I prefer doing it anyway, I find it "cleaner". – VonC Mar 11 '13 at 15:29
  • Im using JIRA. When I do pull again the branch appears. How can I prevent that? – powder366 Jan 13 '15 at 13:42
  • @powder366 not sure: that could be a question of its own. – VonC Jan 13 '15 at 13:55
  • `there's no such concept of local tracking branches, only remote tracking branches.` This isn't quite true--or at least it requires further explanation. Someone _could_ say they have a "local tracking branch" and mean quite literally: "a branch which is locally-stored, and which is also a tracking branch," which would be a _correct_ (albeit somewhat ambiguous) thing to say. So, it's better to say **there's no such thing as a local-tracking branch, but there _is_ such thing as a _locally-stored_ remote-tracking branch, which is precisely what a remote-tracking branch is.** – Gabriel Staples Sep 06 '20 at 04:50
  • One should also note a locally-stored remote-tracking branch, named `remote_name/branch_name` (ex: `origin/my_branch`) is what is updated by `git fetch.` Note/source: I first mentioned my remarks in this comment and the one above [in a GitHub comment here](https://gist.github.com/magnusbae/10182865#gistcomment-3443489). – Gabriel Staples Sep 06 '20 at 04:52
  • 2
    @GabrielStaples I agree. For more on remote tracking: https://stackoverflow.com/a/44081446/6309, https://stackoverflow.com/a/38202006/6309 and https://stackoverflow.com/a/28341622/6309 – VonC Sep 06 '20 at 09:24
276

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

nukeguy
  • 416
  • 1
  • 7
  • 20
Yoshua Wuyts
  • 3,926
  • 1
  • 20
  • 16
  • 13
    If you don't want to remove the current branch, simply: `git branch --unset-upstream [branchname]` – Sonata Oct 19 '16 at 08:36
  • Sure it is, feel free to reply when you find out and I'll happily update the comment – Yoshua Wuyts Jan 07 '17 at 12:21
  • Hmm, I'm getting `fatal: 'origin/master' does not appear to be a git repository fatal: Could not read from remote repository. ` after doing this and trying a git pull origin/master – information_interchange Aug 13 '18 at 15:51
  • And if you want to easily see what remote branches you're tracking, use `git branch -vv` – Daniel Jun 21 '21 at 07:02
  • @information_interchange You should do git pull origin master (space between the two, not /). git-fetch and git-pull want a URL (or its alias, e.g., origin) and a branch (e.g., master). On the other hand, git-log and git-branch want just a branch, i.e., origin/master, other_remote_repo/develop, or my_local_branch. – StephenM Mar 31 '23 at 23:04
111

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.

Alex
  • 9,250
  • 11
  • 70
  • 81
Dobes Vandermeer
  • 8,463
  • 5
  • 43
  • 46
  • 11
    `git branch -d ` 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 `. – Seldom 'Where's Monica' Needy Feb 13 '16 at 08:01
  • @powder366 Depending on your settings, `git pull` may grab **all** remote branches and re-add any it considers to have "appeared" on remote. You can change this "pull all branches" behavior by doing `git config [--global] push.default simple` or `git config [--global] push.default current`. [More on **`push.default`** here](https://git-scm.com/docs/git-config "load the page then ctrlF for push.default"). – Seldom 'Where's Monica' Needy Feb 13 '16 at 08:10
  • @SeldomNeedy They are *not* "old" branches! They're just branches that are no longer tracking the remote ones -- which happens to be the subject of the question. – Marco Feb 14 '16 at 22:32
  • @Marco would you prefer I said "(locally) disused" ? In most cases to most people, this translates as "old". – Seldom 'Where's Monica' Needy Feb 15 '16 at 00:08
  • 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 '16 at 14:00
43

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

Jacob Groundwater
  • 6,581
  • 1
  • 28
  • 42
  • 5
    +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 09 '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
  • 5
    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
  • In my case, which I am not sure anyone will have, I had the new remote branch tracking default set up, and I temporarily switched to a second remote I have set up to the same github repo that is https based instead of my normal ssh based remote because I was on a plane and ssh was not allowed. It automatically set up the remote, and then when not on the plane, I deleted the branch, but I couldn't check it out again because it said there were 2 remote tracking branches. I ended up just removing the remote until next time. – nroose Jul 29 '22 at 17:36
20

Local-tracking branches

If you're talking about local branches (e.g. main, dev) that are configured to push-to and pull-from an upstream [remote branch], then you can disable that with:

❯ git branch --unset-upstream <LOCALBRANCH>

E.g.:

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

Remote-tracking branches

If you're talking about branches of the name <REMOTE>/<BRANCH> (e.g. origin/main, origin/dev) that show up in your git log (and .git/refs/remotes/<REMOTE>/ directory) showing you the state of a remote branch, then you can stop having it "tracked" (having it updated) by overwriting the current list of held remote-tracking branches with your own new custom list:

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

If additionally, you don’t want to see those remote-tracking branches anymore in your git log (and .git/refs/remotes/<REMOTE>/ directory), then you can remove them with:

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

E.g.:

# 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

Stale Remote branches

Finally, if there are remote branches that have been removed from the remote repository itself (have become stale), and you want to update your local repository to reflect that, then you can by deleting (pruning) them:

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

...or

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

PS

You can check the state of tracking with:

❯ git remote show <REMOTE>

E.g.:

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

8c6b5df0d16ade6c
  • 1,910
  • 15
  • 15
  • Is there a way to batch delete remote branches? In my case the branches aren't stale, the remote itself is stale. Doing `git branch --delete --remotes -- staleremote/hotfix` for each branch is tedious. – Mark Jeronimus Mar 06 '23 at 12:37
  • as of August 2023 this is the cleanest answer – maoizm Aug 17 '23 at 05:39
17

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.

Community
  • 1
  • 1
CletusW
  • 3,890
  • 1
  • 27
  • 42
4

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)

Danny Kopping
  • 4,862
  • 2
  • 29
  • 38
  • If only remote-tracking branches are to be untracked (deleted), then I think this is shorter one-line alternative: `git branch -r -D $(git branch -r | grep -v "master")` – dma_k Oct 03 '18 at 12:50
1

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
qneill
  • 1,643
  • 14
  • 18
  • 1
    +1. Nice alias, in addition to [my answer above](http://stackoverflow.com/a/3046478/6309). – VonC Jun 04 '14 at 17:02
0

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

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

Mark Caudill
  • 134
  • 1
  • 1
  • 14
    I'm confused, the OP did not wanted to delete the remote branch. – madth3 Oct 02 '13 at 23:46
  • It is good to clean tracked remote branches from your local repository which was deleted on remote bare repository. – Marek Podyma Mar 29 '17 at 10:36
  • `--prune: Before fetching, remove any remote-tracking references that no longer exist on the remote.` though not an answer to the original question asked it solved my variation of 'How do you stop tracking a remote branch in Git?': how do you stop tracking a remote branch in git which does not exist on remote anymore. – nuala Jun 02 '21 at 11:25
-1

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.

Bob
  • 291
  • 2
  • 8
-4

You can use this way to remove your remote branch

git remote remove <your remote branch name>
Hưng
  • 1
  • 2
    That would remove *all* remote tracking branches for that remote: il you have more than one, that could be problematic. – VonC Jul 11 '19 at 07:49