Changing the Git remote 'push to' default

后端 未结 11 1893
失恋的感觉
失恋的感觉 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 05:52

    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
    0 讨论(0)
  • 2020-12-04 05:56

    Working with Git 2.3.2 ...

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

    Now status, push and pull are pointed to myfork remote

    0 讨论(0)
  • 2020-12-04 05:59

    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

    0 讨论(0)
  • 2020-12-04 06:03

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 06:05

    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)
    
    0 讨论(0)
提交回复
热议问题