Changing the Git remote 'push to' default

后端 未结 11 1903
失恋的感觉
失恋的感觉 2020-12-04 05:35

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

git push

Instead of:

git push upstream
         


        
11条回答
  •  不思量自难忘°
    2020-12-04 06:03

    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.

提交回复
热议问题