Search code examples
gitkeypushconfigdefault

git config default push does not work for remote with name other than origin


If I add remote repository address this way and set it as default:

git init .

remoteName="origin"
dstUrl='location-of-initialized-bare-repository'
git remote add "$remoteName" "$dstUrl"

git config push.default current

touch masterfile
git add masterfile
git commit -m 'first'
git push


git checkout -b feature
touch feautrefile
git add feautrefile
git commit -m 'second'
git push

everything works well. but when i set remote name differently ie:

remoteName="something"

fatal: No configured push destination.

I thought that the name for the remote was arbitrary and could be set to any value without any difference in acting but it seems that for the default remote used for git push without any parameters it must be origin or I'm missing someting? Perhaps git looks for origin by default but in case of different name I need to tell it that the different name is the default?

how to set default remote named differently than origin?

The solution should work with new branches created in the future.


Solution

  • Just adding a new remote URL/name won't enable a local branch to push to it.

    You need to use that new remote at least once with:

    git push -u newRemoteName myBranch 
    

    See "Understanding .git/config's 'remote' and 'branch' sections"

    That does set the branch.myBranch.remote to newRemoteName, which then enable a simple git push to work.

    The first push worked because it defaults to a remote named (by convention) "origin".
    See git push

    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.