268

I want to change the Git default remote branch destination so I could just

git push

Instead of:

git push upstream

Currently this is set to the origin remote and I want to set it to a different remote.

I tried to remove the original (cloned from) remote

git remote rm origin

Which did remove the original remote. But doesn't solve the git push problem. I still get:

fatal: No configured push destination. Either specify the URL from the
command-line or configure a remote repository using...

I also tried to play with:

git remote set-url --push myfork origin

and other options but none seem to work (maybe because I deleted the origin remote too soon?)

Following the answer here I tried to change:

git config push.default upstream (or matching)

but neither worked.

Community
  • 1
  • 1
alonisser
  • 10,004
  • 16
  • 71
  • 123
  • Does this answer your question? [Different default remote (tracking branch) for git pull and git push](https://stackoverflow.com/questions/2916845/different-default-remote-tracking-branch-for-git-pull-and-git-push) – BuZZ-dEE Jul 08 '20 at 20:16
  • @BuZZ-dEE you realize you just tried to point me to an answer for a question I've asked 6 years ago right? – alonisser Jul 23 '20 at 18:37
  • yes, but my comment is the default message, when you flag a question as a duplicate. I did it, because I think this question should be closed as a duplicate of the question linked in my comment. – BuZZ-dEE Jul 23 '20 at 20:16
  • The question was asked 3 years before your question. – BuZZ-dEE Jul 25 '20 at 21:28
  • Lol, true that, but again, this behavior is between extreme nitpicking to plain abusive, closing a 7 year old question, and doesn't help the community a bit – alonisser Jul 27 '20 at 05:31
  • I think it does help the community.If someone searches for this topic, the related questions are linked. – BuZZ-dEE Jul 27 '20 at 07:54
  • Has this been answered? Because I still can't get it to permanently set the default remote branch for master (for git push --all) , because "push -u" doesn't: git push -u "origin" "master:RELEASE12345" # Does not set it up permanently If there is something to manually edit in .git/config I'm confortable with that. – Malcolm Boekhoff Oct 02 '20 at 01:32
  • Very useful, Thanks :) – CDBROUK Oct 13 '20 at 14:24

13 Answers13

250

You can use git push -u <remote_name> <local_branch_name> to set the default upstream. See the documentation for git push for more details.

1615903
  • 25,960
  • 9
  • 55
  • 82
  • 1
    Doesn't work: I get '''fatal: 'origin' does not appear to be a git repository fatal: The remote end hung up unexpectedly''' maybe because I deleted origin remote before moving on. just found a working solution, I'll update in an answer – alonisser Sep 14 '13 at 11:28
  • 37
    As an additional clarification, the syntax should be `git push -u :`. – Marco Lazzeri Apr 23 '14 at 19:35
  • 35
    Example: `git push -u origin master:master`. – starbeamrainbowlabs Oct 17 '15 at 14:14
  • 1
    After setting a default remote...isn't there anyway that you can force `git push` to push remote branch of the current local branch? I mean doing it this means I have to run this command for every branch. Right? Can't I just do an initial setup for the entire repo? @MarcoLazzeri – Honey Nov 14 '16 at 15:50
  • How big is the branch problem? Assuming it's just for this repo, and you have Gnu Tools available: cat .git/config | grep -A 1 -e "^\\[branch" – Robert Echlin Jul 13 '17 at 14:47
  • That command shows all the branches in the repo and what remote they default to. – Robert Echlin Jul 13 '17 at 14:53
  • 2
    @starbeamrainbowlabs Isn't `git push -u origin master` sufficient? Are you just illustrating the full syntax? – Josiah Yoder Jul 29 '19 at 21:24
  • @JosiahYoder Indeed, just an illustration of the above comment. – starbeamrainbowlabs Jul 30 '19 at 13:40
  • Note that it does not only set up the default upstream but also makes the push. – Jean Paul May 22 '20 at 13:58
  • What about `git push -u origin head`? Why not use that? https://stackoverflow.com/a/66663502/470749 – Ryan Mar 16 '21 at 21:14
120

To change which upstream remote is "wired" to your branch, use the git branch command with the upstream configuration flag.

Ensure the remote exists first:

git remote -vv

Set the preferred remote for the current (checked out) branch:

git branch --set-upstream-to <remote-name>

Validate the branch is setup with the correct upstream remote:

git branch -vv

Jordan McCullough
  • 1,971
  • 1
  • 11
  • 7
  • 2
    Just tried ```git branch --set-upstream-to myfork``` and got an error: >error: unknown option `set-upstream-to' I'm running git 1.7.9 – alonisser Sep 15 '13 at 21:17
  • 3
    The syntax changed in 1.8, see http://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch – jtniehof May 07 '15 at 12:01
  • 1
    See the following answer: http://stackoverflow.com/a/2432799/1820106 (`git remote set-url origin PATH_TO_REMOTE`) – Yinon Ehrlich Sep 19 '16 at 07:49
  • 12
    `git branch -u /` worked for me. The command was run with the local branch of interest checked out. See 'Tracking Branches' in https://git-scm.com/book/it/v2/Git-Branching-Remote-Branches – norio Feb 09 '17 at 04:50
  • This is better than the accepted answer because it doesn't actually do a push (i.e., no unintended side-effects). – ijoseph Jan 22 '18 at 22:43
  • 2
    The remote command verbose output is really just `git remote -v` or `git remote --verbose`. The `-vv`'s extra v is redundant. – Artif3x Feb 22 '18 at 19:37
  • Instead of `git branch -vv` use `git branch -vv --list ` to show only the specific branch (``) – Murmel Nov 21 '18 at 10:07
  • Make sure that you have previously fetched the remote repository! – Marko Mar 23 '20 at 15:42
63

Working with Git 2.3.2 ...

git branch --set-upstream-to myfork/master

Now status, push and pull are pointed to myfork remote

pinei
  • 1,641
  • 1
  • 20
  • 23
43

You can easily change default remote for branches all at once simple using this command

git push -u <remote_name> --all
Mykola Denysyuk
  • 1,731
  • 13
  • 15
20

If you did git push origin -u localBranchName:remoteBranchName and on sequentially git push commands, you get errors that then origin doesn't exist, then follow these steps:

  1. git remote -v

Check if there is any remote that I don't care. Delete them with git remote remove 'name'

  1. git config --edit

Look for possible signs of a old/non-existent remote. Look for pushdefault:

[remote]
  pushdefault = oldremote

Update oldremote value and save.

git push should work now.

Șerban Ghiță
  • 1,751
  • 18
  • 21
  • 2
    Thanks! I tend to add `-u` when using `git push` to a different remote due to muscle memory. This makes the given remote the default, setting it back using `git config --edit` (or pushing again and setting a new remote) solved the problem. – Tim Visée Mar 20 '19 at 18:33
  • Thank you! This is really helpful since usually I change the name of the origin to something that is more descriptive. Honestly I think this is the better answer since it seems like the person asking the question is trying to re-define origin/default remote repo. – codejedi365 Dec 27 '20 at 21:46
13

Just a clarification (using git version 1.7.9.5 on ubuntu 12.04):

Git will add/remove remotes. These are remote instances of git with a server attached.

git remote add myremote git://remoteurl

You can then fetch said git repository like so:

git fetch myremote

It seems this creates a branch named 'myremote', however the remote for the branch is not automatically set. To do this, you must do the following:

First, verify that you have this problem, i.e.

git config -l | grep myremote

You should see something like:

remote.myremote.url=git://remoteurl
remote.myremote.fetch=+refs/heads/*:refs/remotes/myremote/*
branch.myremote.remote=.
branch.myremote.merge=refs/heads/master

If you see branch.myremote.remote=. , then you should proceed:

git config branch.myremote.remote myremote
git checkout myremote
git pull

You should now be up to date with the remote repository, and your pulls/pushes should be tied to the appropriate remote. You can switch remotes in this manner, per branch. [Note][1]

According to a The Official Git Config Documentation, you can set up a default push branch (just search remote.pushdefault on that page), however keep in mind that this will not affect repositories/branches which already exist, so this will work but only for new repositories/branches. You should remember that --global will set user-specific repository defaults (~/.gitconfig), --system will set system-wide repository defaults (/etc/gitconfig), and no flag will set configuration options for the current repository (./.gitconfig).

Also it should be noted that the push.default config option is for configuring ref-spec behavior, not remote behavior.

[1]: git branch --set-upstream myotherremote would usually work here, however git will complain that it will not set a branch as its own remote if git branch --set-upstream myremote is used. I believe however that this is incorrect behavior.

smaudet
  • 581
  • 6
  • 16
  • Instead of `git config -l | grep myremote`, you can simply use: `git config --get branch.myremote.remote` – Murmel Nov 21 '18 at 10:02
  • @Murmel first, this is a community wiki so feel free to improve, second, `--get` assumes particular behavior, I make a note that there are other ways to set upstreams but this provides a way to find the information without only relying on git tooling to be correct. Git isn't perfect, its better to understand the data, what it does and then hope its invocations work appropriately. – smaudet Nov 23 '18 at 04:30
13

It might be helpful to take a look at .git/config inside your repo, it will list all remotes and also the default remote for each branch

eg.

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@gitea.xxx.be:fii/web2016.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "bugfix/#8302"]
    remote = origin
    merge = "refs/heads/bugfix/#8302"
[branch "feature/#8331"]
    remote = origin
    merge = "refs/heads/feature/#8331"
[remote "scm"]
    url = https://scm.xxx.be/git/web2016bs.git
    fetch = +refs/heads/*:refs/remotes/scm/*

you can make manual changes in this file to remove an unwanted remote, or update the default remotes for the different branches you have

  • Pay attention! when changing or removing the remotes make sure to update all references to it in this config file
Kim Paulissen
  • 358
  • 3
  • 5
9

In my case, I fixed by the following: * run git config --edit * In the git config file:

[branch "master"]
remote = origin # <--- change the default origin here
Ron
  • 4,250
  • 3
  • 27
  • 41
8

Another technique I just found for solving this (even if I deleted origin first, what appears to be a mistake) is manipulating git config directly:

git config remote.origin.url url-to-my-other-remote
alonisser
  • 10,004
  • 16
  • 71
  • 123
  • 6
    This is not a good solution, git users should be able to pull/push from multiple repositories - although this will allow you to change the original's remote, it does not mean that you SHOULD change the original's remote, since this will likely mean that there are a number of incompatibilities between remotes. Merge will help here, but it is both simpler and preserves more history to set the remote on the new branch. – smaudet Jan 01 '14 at 17:47
  • Not the proper way of doing it... @Jordan's answer is more apropriate – Raja Anbazhagan May 01 '16 at 20:05
  • 1
    See also: http://stackoverflow.com/a/2432799/1820106 (`git remote set-url origin PATH_TO_REMOTE`) – Yinon Ehrlich Sep 19 '16 at 07:52
5

Very simply, and cobbling together some of the great comments here along with my own research into this.

First, check out the local branch you want to tie to your remote branch:

git checkout mybranch

Next:

git branch -u origin/mybranch

where:

git branch -u {remote name}/{branch name}

You should get a message:

"Branch mybranch set up to track remote branch mybranch from origin."
Artif3x
  • 3,205
  • 1
  • 21
  • 19
0

git remote set-url --push origin should work, as you mentioned, but you need to explicitly provide the url instead of an alternative remote name, e.g.

git remote set-url --push origin git@github.com:contributor/repo.git

You can confirm whether this worked by doing a git remote -v. E.g.

λ ~/go/src/github.com/stretchr/testify/ master git remote -v
fork    git@github.com:contributor/testify.git (fetch)
fork    git@github.com:contributor/testify.git (push)
origin  git@github.com:stretchr/testify (fetch)
origin  git@github.com:contributor/testify.git (push)
be_es
  • 1
0

Like docs say:

When the command line does not specify where to push with the <repository> argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin.

0

git push -u origin head is what I was looking for.


Here is what it solves for me:

fatal: The current branch task/PLAT-1924-datagrid-tool-panel-scrollbar has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin task/PLAT-1924-datagrid-tool-panel-scrollbar

➜  frontend git:(task/PLAT-1924-datagrid-tool-panel-scrollbar) git push -u origin head

Instead of using my mouse to go up and copy the suggestion from Git (git push --set-upstream origin task/PLAT-1924-datagrid-tool-panel-scrollbar) and then paste that and run it, I can use a keyboard shortcut or alias to type git push -u origin head, and it doesn't need to be branch-specific.

Thanks to https://stackoverflow.com/a/23402125/470749.

Ryan
  • 17,332
  • 24
  • 141
  • 270