更改 Git 远程“推送到”默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18801147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the Git remote 'push to' default
提问by alonisser
I want to change the Git default remote branch destination so I could just
我想更改 Git 默认远程分支目标,这样我就可以
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:
这确实删除了原来的遥控器。但并不能解决git push
问题。我仍然得到:
fatal: No configured push destination. Either specify the URL from the
command-line or configure a remote repository using...
致命:未配置推送目标。
从命令行指定 URL或使用...配置远程存储库
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 hereI tried to change:
git config push.default upstream (or matching)
but neither worked.
但都没有奏效。
采纳答案by alonisser
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:
我刚刚发现的另一种解决此问题的技术(即使我先删除了 origin,这似乎是一个错误)是直接操作 git config:
git config remote.origin.url url-to-my-other-remote
回答by 1615903
You can use git push -u <remote_name> <local_branch_name>
to set the default upstream. See the documentation for git pushfor more details.
您可以使用git push -u <remote_name> <local_branch_name>
设置默认上游。有关更多详细信息,请参阅git push的文档。
回答by Jordan McCullough
To change which upstream remote is "wired" to your branch, use the git branch
command with the upstream configuration flag.
要更改哪个上游远程“连接”到您的分支,请使用git branch
带有上游配置标志的命令。
Ensure the remote exists first:
首先确保遥控器存在:
git remote -vv
git remote -vv
Set the preferred remote for the current (checked out) branch:
为当前(签出)分支设置首选远程:
git branch --set-upstream-to <remote-name>
git branch --set-upstream-to <remote-name>
Validate the branch is setup with the correct upstream remote:
使用正确的上游远程验证分支设置:
git branch -vv
git branch -vv
回答by pinei
Working with Git 2.3.2 ...
使用 Git 2.3.2 ...
git branch --set-upstream-to myfork/master
Now status
, push
and pull
are pointed to myfork
remote
现在status
,push
并pull
指向myfork
远程
回答by Mykola Denysyuk
You can easily change default remote for branches all at once simple using this command
您可以使用此命令轻松地一次性更改分支的默认远程
git push -u <remote_name> --all
回答by ?erban Ghi??
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:
如果您执行git push origin -u localBranchName:remoteBranchName
并按顺序执行git push
命令,您会收到错误,然后原点不存在,然后按照以下步骤操作:
git remote -v
git remote -v
Check if there is any remote that I don't care.
Delete them with git remote remove 'name'
检查是否有任何我不在乎的遥控器。删除它们git remote remove 'name'
git config --edit
git config --edit
Look for possible signs of a old/non-existent remote.
Look for pushdefault
:
寻找旧的/不存在的遥控器的可能迹象。寻找pushdefault
:
[remote]
pushdefault = oldremote
Update oldremote
value and save.
更新oldremote
值并保存。
git push
should work now.
git push
现在应该工作。
回答by smaudet
Just a clarification (using git version 1.7.9.5 on ubuntu 12.04):
只是澄清(在 ubuntu 12.04 上使用 git 版本 1.7.9.5):
Git will add/remove remotes. These are remote instances of git with a server attached.
Git 将添加/删除遥控器。这些是附加了服务器的 git 远程实例。
git remote add myremote git://remoteurl
You can then fetch said git repository like so:
然后你可以像这样获取所说的 git 存储库:
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:
似乎这会创建一个名为“myremote”的分支,但是不会自动设置该分支的远程。为此,您必须执行以下操作:
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:
如果您看到branch.myremote.remote=.
,那么您应该继续:
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]
您现在应该与远程存储库保持同步,并且您的拉取/推送应该绑定到适当的远程。您可以按这种方式切换遥控器,每个分支。[注意][1]
According to a The Official Git Config Documentation, you can set up a defaultpush 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).
根据The Official Git Config Documentation,您可以设置默认推送分支(只需在该页面上搜索 remote.pushdefault ),但请记住,这不会影响已经存在的存储库/分支,因此这将起作用,但仅对于新的存储库/分支。你应该记住,这--global
将设置用户特定的存储库默认值 (~/.gitconfig),--system
将设置系统范围的存储库默认值 (/etc/gitconfig),并且没有标志将为当前存储库设置配置选项 (./.gitconfig)。
Also it should be noted that the push.defaultconfig option is for configuring ref-specbehavior, notremotebehavior.
还应该注意的是,push.default配置选项用于配置ref-spec行为,而不是远程行为。
[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.
[1]:git branch --set-upstream myotherremote
通常会在这里工作,但是 git 会抱怨如果使用它,它不会将分支设置为自己的远程git branch --set-upstream myremote
。然而,我相信这是不正确的行为。
回答by Kim Paulissen
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
查看.git/config
您的存储库内部可能会有所帮助,它将列出所有遥控器以及每个分支的默认遥控器
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
- 请注意!更改或删除遥控器时,请确保更新此配置文件中对它的所有引用
回答by Artif3x
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."
回答by Ron
In my case, I fixed by the following:
* run git config --edit
* In the git config file:
就我而言,我通过以下方式修复: * run git config --edit
* 在 git 配置文件中:
[branch "master"]
remote = origin # <--- change the default origin here