如何轻松地将本地 Git 分支推送到具有不同名称的远程?
我一直想知道是否有一种简单的方法可以使用具有不同名称的远程分支来推送和拉取本地分支,而无需始终指定两个名称。
例如:
$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name
现在,如果有人更新了remote_branch_name,我可以:
$ git pull
并且所有内容都会被合并/快进。但是,如果我在本地“新手”中进行更改,我不能:
$ git push
相反,我必须:
% git push origin newb:remote_branch_name
看起来有点傻。如果git-pull
使用git-configbranch.newb.merge
来确定从哪里拉取,为什么git-push
不能有一个类似的配置选项?有没有一个好的捷径,或者我应该继续走很长的路?
I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.
For example:
$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name
Now if someone updates remote_branch_name, I can:
$ git pull
And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:
$ git push
Instead, I have to:
% git push origin newb:remote_branch_name
Seems a little silly. If git-pull
uses git-config branch.newb.merge
to determine where to pull from, why couldn't git-push
have a similar config option? Is there a nice shortcut for this or should I just continue the long way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当您执行初始推送时,添加 -u 参数< /a>:
后续推送将到达您想要的位置。
编辑:
根据评论,这只设置了拉力。
应该这样做。
When you do the initial push add the -u parameter:
Subsequent pushes will go where you want.
EDIT:
As per the comment, that only sets up pull.
should do it.
当然。只需将
push.default
设置为upstream
即可将分支推送到上游(与pull
从中拉取的内容相同,由定义) >branch.newb.merge
),而不是将分支推送到名称匹配的分支(这是push.default
、matching
的默认设置)。请注意,在 Git 1.7.4.2 之前,这被称为
tracking
而不是upstream
,因此,如果您使用的是旧版本的 Git,请使用tracking
> 相反。push.default
选项是在 Git 1.6.4 中添加的,因此如果您使用的是比该版本更旧的版本,则根本没有此选项,并且需要显式指定分支推至。Sure. Just set your
push.default
toupstream
to push branches to their upstreams (which is the same thatpull
will pull from, defined bybranch.newb.merge
), rather than pushing branches to ones matching in name (which is the default setting forpush.default
,matching
).Note that this used to be called
tracking
notupstream
before Git 1.7.4.2, so if you're using an older version of Git, usetracking
instead. Thepush.default
option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.Adam 的命令现已弃用。您可以使用:
将
my_local_branch
的上游分支设置为origin/my_remote_branch
。The command by Adam is now deprecated. You can use:
to set the upstream branch of
my_local_branch
toorigin/my_remote_branch
.摘要:
这里是您通常需要的关键命令的简短摘要:
详细信息:
下面按顺序介绍以下部分:
太多这里的不完整和部分答案给我留下了很多问题和很多不足之处。因此,经过大量的努力、研究和实验,我尝试提供一个完整的解决方案。
1.从本地分支推送到具有不同名称的远程分支
要从本地
branch2
推送到远程branch1
,您必须同时指定像这样的分支:但是请注意,我以上面的一般形式编写的方括号表示
:to_branch
部分是可选。我的意思是,要从具有一个名称的本地分支推送到具有不同名称的远程分支,该部分不是可选的,但是,作为一般 git 命令,如果不包含,该命令将运行:to_branch
部分,这意味着它在这个意义上是可选的。但是,却可能会产生意想不到的结果!看一下这个命令,例如:您当前可能已签出本地
branch3
,并认为git push originbranch2
将推送您的本地branch3< /code> 到远程
branch2
,因为您当前在系统上签出了branch3
,但这不会发生!相反,git push originbranch2
会将本地branch2
推送到远程branch2
,即使您没有>branch2
当前已签出!git push originbranch2
因此是一个等效的简写形式:如果您使用上面的 cmd 的简写形式,则会产生非常令人困惑的行为认为它会从您当前签出的分支推送。这是一个 Nota bene 注释,总结了上述行为:
注意:采用一般形式
git push <远程>[:to_branch]
,如果您没有使用:to_branch
指定远程 TO 分支,则假定它与本地 FROM 分支同名,from_branch
,在远程
!这意味着如果您仅输入 git push originbranch2 而不是 git push origin some_other_branch:branch2 ,它会从本地branch2
推送到远程副本branch2
,即使您在运行命令时没有在本地签出branch2
!如果您认为输入git push originbranch2
只是告诉您当前签出的分支some_other_branch
推送到< code>branch2 位于远程,而本地branch2
被推送到远程branch2
。通用形式的文档 ([:to_branch]) 很难找到,但实际上可以在
branch2
>git push顶部附近的...
”部分:man git push
页面中找到“然后后来:
我认为这个文档是不直观的,并且非常难以理解,但是,没有一些示例和我上面的解释。
[
git push
的更好形式]您还可以在推送的同时设置上游分支:作为命令输出的一部分上面,你应该看到:
为了清楚地表明那里发生了什么,请知道上面的两个命令中的任何一个都相当于这两个单独的命令:
现在,查看您的分支的上游分支当前设置为< /strong>,运行双详细 (
-vv
)git分支
cmd:示例输出:
这里可以看到上游分支是
origin/master
,这意味着名为origin
的远程上的master
分支:注意:
-vv
表示“双重详细”。这意味着它不仅会详细地打印 gitbranch,而且会双重详细地打印,或者更加详细地打印。现在打印的“额外详细”内容包括方括号中的上游分支,如上所示:[origin/matser]
。origin
是上面示例中显示的遥控器。2. 从与本地分支名称不同的远程分支中拉取
[如果您已在本地签出分支
branch2
,则推荐使用!] 要从 < code>branch1 在名为origin
的远程服务器上,TObranch2
,您必须指定要从中提取的远程分支,如下所示:您还可以指定两个分支,但我不完全确定在这种情况下有什么区别:
以下命令仅在远程和本地分支具有相同名称的情况下才有效! em> (因此它不回答这个堆栈溢出问题)。如果您尚未签出分支
some_branch
,建议使用此命令!注意:
git Push
不同,git pull
确实如此没有-u
选项。git fetch origin some_branch:some_branch
命令使用在命令的两个位置中使用两次的相同some_branch
名称来完成。区别在于,git fetch origin some_branch
仅使用最新版本更新名为origin/some_branch
的本地存储、隐藏的远程跟踪分支。存储在名为origin
的远程服务器上的按此名称进行的分支的更改,而git fetch origin some_branch:some_branch
则可以更新本地存储的可见some_branch
也进行了这些更改。some_branch
,您实际上拥有最多3 个分支:1) 本地分支some_branch
,2) 名为origin
的远程服务器上的远程分支some_branch
和 3) 以及名为origin/some_branch
的本地存储、隐藏、远程跟踪分支。阅读此处了解更多信息。我第一次了解到每个分支3个分支的概念:如何本地和远程删除 Git 分支?。另请参阅我的评论,在该答案下。3. 配置本地分支以跟踪或取消跟踪远程分支
您可以设置名为
branch2
的本地分支来跟踪名为的上游分支 >branch1
与推送同时,使用上面显示的git push -u
cmd。您还可以设置名为
branch2
的本地分支来跟踪名为branch1
的上游分支,如下所示:取消设置您的上游分支 < code>branch2,因此它不再跟踪先前设置的上游分支(在上面的示例中是
origin/branch1
),运行以下命令:如上所示,要查看分支的上游分支当前设置为,请运行double-verbose (
-vv
)git 分支
cmd:参考资料:
我写过的相关
git
主题:Summary:
Here is a short summary of just the key commands you need in general:
Details:
The following sections are covered below, in this order:
There are too many incomplete and partial answers here which leave me with a lot of questions and a lot to be desired. So, after a bunch of effort and research and experimenting, here is my attempt at providing a complete solution.
1. Pushing from your local branch to a remote branch with a different name
To push FROM your local
branch2
TO remotebranch1
, you must specify both branches like this:Notice, however, that the square brackets I have written in the general form above indicate the
:to_branch
part is optional. What I mean is that to push from a local branch with one name to a remote branch with a different name, that part is NOT optional, but, as a general git command, the command will run if you do not include the:to_branch
part, meaning it is optional in that sense. But, it may produce unexpected results! Take a look at this command, for example:You might have local
branch3
currently checked-out, and think thatgit push origin branch2
will push your localbranch3
to the remotebranch2
, since you havebranch3
currently checked-out on your system, but this is NOT what will happen! Rather,git push origin branch2
will push your localbranch2
to your remotebranch2
, again, even if you do NOT havebranch2
currently checked-out!git push origin branch2
is therefore an equivalent short-hand of this:The short form of the cmd just above produces very confusing behavior if you think it will push from your currently-checked-out branch instead. Here is a Nota bene note summarizing the behavior described above:
NB: In the general form
git push <remote> <from_branch>[:to_branch]
, if you don't specify the remote TO branch with:to_branch
, it is assumed to be the same name as your local FROM branch,from_branch
, on theremote
! This means if you type onlygit push origin branch2
instead ofgit push origin some_other_branch:branch2
, it pushes FROM your localbranch2
TO the remote copy ofbranch2
, EVEN IF YOU DIDNT HAVEbranch2
locally checked-out at the time of running the command! This can be VERY CONFUSING if you thought typinggit push origin branch2
had just told your currently-checked out branch,some_other_branch
, to push tobranch2
on the remote and instead, the localbranch2
got pushed to the remotebranch2
.The documentation for the general form (
git push <remote> <from_branch>[:to_branch]
) is hard to find, but it's actually found in theman git push
pages near the top under the"<refspec>...
" section:And then later:
I think this documentation is non-intuitive and very difficult to understand, however, without some examples and my explanation above.
[BETTER FORM OF
git push
] You can also set the upstream branch at the same time as pushing:As part of the output of the command above, you should see:
To make it obvious what is happening there, know that either of the two commands just above are equivalent to these two separate commands:
Now, to view what your branch's upstream branch is currently set to, run the double-verbose (
-vv
)git branch
cmd:Sample output:
Here you can see that the upstream branch is
origin/master
, which means themaster
branch on the remote namedorigin
:Notes:
-vv
above means "double verbose". This means it will printgit branch
not just verbosely, but double verbosely, or extra verbosely. The "extra verbose" content now printed includes the upstream branch in square brackets, as shown above:[origin/matser]
.git remote -v
.origin
is the remote shown in the examples above.2. Pulling from a remote branch with a different name to your local branch
[Recommended if you already have branch
branch2
checked-out locally!] To pull FROMbranch1
on the remote namedorigin
, TObranch2
, you must specify the remote branch to pull from, like this:You can also specify both branches, but I'm not entirely sure what the difference is in this case:
The following command only works if the remote and local branches have the same name! (therefore it does NOT answer this Stack Overflow question). This command is recommended if you do NOT already have branch
some_branch
checked-out!Notes:
git push
,git pull
does NOT have a-u
option.git fetch origin some_branch:some_branch
command is done with the samesome_branch
name used twice--in both locations in the command. The difference is simply thatgit fetch origin some_branch
only updates the locally-stored, hidden, remote-tracking branch namedorigin/some_branch
with the latest changes from the branch by this name stored on the remote server namedorigin
, whereasgit fetch origin some_branch:some_branch
does that PLUS also updates the locally-stored visiblesome_branch
with those changes too.some_branch
you think you have, you actually have up to 3 branches: 1) a local branchsome_branch
, 2) a remote branchsome_branch
on a remote server namedorigin
, and 3) and locally-stored, hidden, remote-tracking branch namedorigin/some_branch
. Read here for more info. and for where I first learned this concept of 3 branches per branch: How do I delete a Git branch locally and remotely?. See also my comment here, under that answer.3. Configuring your local branch to track or untrack a remote branch
You can set your local branch named
branch2
to track an upstream branch namedbranch1
at the same time as pushing by using thegit push -u
cmd shown above.You can also set your local branch named
branch2
to track an upstream branch namedbranch1
like this:To UNset your upstream branch for
branch2
, so it no longer tracks the previously-set upstream branch (which wasorigin/branch1
in the examples above), run this:And again, as already shown above, to view what your branch's upstream branch is currently set to, run the double-verbose (
-vv
)git branch
cmd:References:
git push -u origin local_FROM_branch:remote_TO_branch
syntax: @Adam Dymitruk's answerRelated
git
topics I've written about:如何推送到 Git 上不同名称的分支
您通常会将本地分支推送到同名的远程分支,但并非总是如此。
要推送到不同名称的分支,您只需指定
要推送的分支
以及要推送到的分支的名称,并用冒号 (:) 分隔。例如,如果您想将名为
some-branch
的分支推送到my-feature
:如何将所有本地分支推送到远程
不需要经常从本地推送所有分支,但如果需要,可以添加
--all
标志:How to push to a branch of a different name on Git
You will usually push your local branch to a remote branch of the same name—but not always.
To push to a branch of a different name, you just need to specify the
branch you want to push
and the name of the branch you want to push to separated by a colon (:).For example, if you want to push a branch called
some-branch
tomy-feature
:How to push all local branches to the remote
You won't need to push all branches from your local very often, but if you do you can add the
--all
flag:是的,有一个配置选项可以让 git 默认推送到上游分支。
使用以下一组语句,这样您就不必每次都执行
git push origin local:remote
:将当前分支的上游设置为具有不同名称的远程分支后,您需要更改
git Push
到upstream
的默认行为(请参阅git-config
的联机帮助页)。现在,
git push
将遵守这些规则并推送到上游集合。Yes, there is a config option to make
git
push to upstream branches by default.Use the following set of statements so you don't have to do
git push origin local:remote
every time:After setting the current branch's upstream to a remote branch with a different name, you need to change the default behavior of
git push
toupstream
(see manpage forgit-config
).Now,
git push
will obey these rules and push to the set upstream.推送并创建临时远程分支
如果您想:
那么就这么简单:
注意:您可以将
HEAD
替换为任何其他分支或提交 ID 来推送它。Push and create a temporary remote branch
If you want to:
Then it's as simple as this:
Note: You can replace
HEAD
with any other branch or commit ID to push that instead.两个选项:
push
所有存储库的分支到通过push.default=upstream
远程。<名称>.push< /code>
要让所有存储库的分支推送到它们从中提取的分支,请使用:
注意:这也会影响其他分支。
要设置单个分支的远程推送对(例如:本地
main
到origin/master
):设置分支的远程
设置远程的推送映射
Two options:
push
all the repo's branches to where they get pulled from viapush.default = upstream
remote.<name>.push
To have all your repo's branches push to the branch that they pull from, use:
Note: this will affect other branches, also.
To setup a single branch's remote push pair (Example: local
main
toorigin/master
):Set the branch's remote
Set the remote's push mapping
这是对我有用的过程。
现在,您的新存储库将是“原始”,原始存储库将是“上游”。通过运行 git Remote -v 进行确认。 (旁注:Upstream 用于从原始存储库中获取 - 以便使本地副本与您想要贡献的项目保持同步 - 而 origin 用于拉取和推送,因为您可以为自己的存储库做出贡献)。
git push origin master
现在,您的新远程存储库的 master(在 Github 上)将与原始 master 同步,但它不会有任何功能分支。
Rebase 是一种智能合并。然后再次推送到 master,您将在新存储库上看到所选功能分支作为 master。
选修的:
Here's the process that has worked for me.
Now your new repo will be ‘origin’ and the original repo is ‘upstream’. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).
git push origin master
Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.
Rebase is a smart merge. Then push to master again and you’ll see the selected feature branch as master on the new repo.
Optional:
如果您的本地分支中有一堆文件想要保存到不同的分支,您可以做的一件很酷的事情是执行 git stash 来保存所有修改过的文件(这是存储库) Wide),然后执行 git checkout remoteBranchYouWant ,然后执行 git stash pop ,现在所有修改的文件都已在新签出的分支中准备就绪,您可以提交并根据需要将它们推送到新分支。
One cool thing you can do if you have a bunch of files in your local branch that you want to save to a different branch, is to do a
git stash
to save all your modified files (this is repository wide), then you do agit checkout remoteBranchYouWant
then do agit stash pop
and now all your modified files are ready in your newly checked-out branch and you can commit and push these up to the new branch as you wish.