Changing the Git remote 'push to' default

GitGit PushGit Remote

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

Working with Git 2.3.2 ...

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

Now status, push and pull are pointed to myfork remote

Solution 4 - Git

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

git push -u <remote_name> --all

Solution 5 - Git

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.

Solution 6 - Git

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 = [email protected]: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

Solution 7 - Git

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.

Solution 8 - Git

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

Solution 9 - Git

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

Solution 10 - Git

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

Solution 11 - Git

In the git man pages, you'll find the following:

>remote.pushDefault
The remote to push to by default. Overrides branch.<name>.remote for all branches, and is overridden by branch.<name>.pushRemote for specific branches.

Solution 12 - Git

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	[email protected]:contributor/testify.git (fetch)
fork	[email protected]:contributor/testify.git (push)
origin	[email protected]:stretchr/testify (fetch)
origin	[email protected]:contributor/testify.git (push)

Solution 13 - Git

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.

UPDATE: Oddly, today I got this error. Changing lowercase head to uppercase in the above command solved the problem, though.

Thanks to https://stackoverflow.com/a/23402125/470749.</sub>

Solution 14 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalonisserView Question on Stackoverflow
Solution 1 - Git1615903View Answer on Stackoverflow
Solution 2 - GitJordan McCulloughView Answer on Stackoverflow
Solution 3 - GitpineiView Answer on Stackoverflow
Solution 4 - GitMykola DenysyukView Answer on Stackoverflow
Solution 5 - GitȘerban GhițăView Answer on Stackoverflow
Solution 6 - GitKim PaulissenView Answer on Stackoverflow
Solution 7 - GitsmaudetView Answer on Stackoverflow
Solution 8 - GitalonisserView Answer on Stackoverflow
Solution 9 - GitRonView Answer on Stackoverflow
Solution 10 - GitArtif3xView Answer on Stackoverflow
Solution 11 - GitBenningtonView Answer on Stackoverflow
Solution 12 - Gitbe_esView Answer on Stackoverflow
Solution 13 - GitRyanView Answer on Stackoverflow
Solution 14 - GitAndre BellafronteView Answer on Stackoverflow