[英]How can I push a local Git branch to a remote with a different name easily?
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:现在,如果有人更新 remote_branch_name,我可以:
$ git pull
And everything is merged / fast-forwarded.一切都被合并/快进。 However, if I make changes in my local "newb", I can't:
但是,如果我在本地“newb”中进行更改,则不能:
$ 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?如果
git-pull
使用git-config branch.newb.merge
来确定从哪里拉,为什么git-push
不能有类似的配置选项? Is there a nice shortcut for this or should I just continue the long way?对此有什么好的捷径,还是我应该继续走很长的路?
When you do the initial push add the -u parameter :当您进行初始推送时,添加-u 参数:
git push -u origin my_branch:remote_branch
Subsequent pushes will go where you want.随后的推送将去到你想要的地方。
EDIT:编辑:
As per the comment, that only sets up pull.根据评论,这只会设置拉力。
git branch --set-upstream
should do it.应该这样做。
Sure.当然。 Just set your
push.default
to upstream
to push branches to their upstreams (which is the same that pull
will pull from, defined by branch.newb.merge
), rather than pushing branches to ones matching in name (which is the default setting for push.default
, matching
).只需将
push.default
设置为upstream
即可将分支推送到它们的上游(这与pull
将拉取的相同,由branch.newb.merge
定义),而不是将分支推送到名称匹配的分支(这是push.default
, matching
)。
git config push.default upstream
Note that this used to be called tracking
not upstream
before Git 1.7.4.2, so if you're using an older version of Git, use tracking
instead.请注意,在 Git 1.7.4.2 之前,这曾经被称为
tracking
而不是upstream
,因此如果您使用的是旧版本的 Git,请改用tracking
。 The push.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. push.default
选项是在 Git 1.6.4 中添加的,因此如果您使用的是比该版本更旧的版本,则根本没有此选项,并且需要明确指定要推送到的分支。
Here's the process that has worked for me.这是对我有用的过程。
git clone original-repo-url
git remote rename origin upstream
git remote add origin new-repo-url
Now your new repo will be 'origin' and the original repo is 'upstream'.现在您的新仓库将是“起源”,而原始仓库是“上游”。 Confirm it by running git remote -v.
通过运行 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.现在你的新远程仓库的 master(在 Github 上)将与原始 master 同步,但它不会有任何功能分支。
git rebase upstream/branch-name
git push origin master
Rebase is a smart merge. Rebase 是一个智能合并。 Then push to master again and you'll see the selected feature branch as master on the new repo.
然后再次推送到 master,您将在新 repo 上看到所选的功能分支作为 master。
Optional:可选的:
git remote rm upstream
git remote add upstream new-repo-url
I have been running into the same issue for quite sometime now.我已经遇到了同样的问题很长一段时间了。 I finally have a set of statements so I don't have to do
git push origin local:remote
every time.我终于有了一组语句,所以我不必每次都执行
git push origin local:remote
。 I followed these:我遵循了这些:
git branch --set-upstream-to origin/remote_branch_name
git config push.default upstream
git push
After setting upstream to a remote branch with different name (1st line) and then making that upstream as default (2nd line), 3rd line will now obey these rules and push to the set upstream.将上游设置为具有不同名称的远程分支(第一行),然后将该上游设置为默认值(第二行)后,第三行现在将遵守这些规则并推送到上游设置。
How to push to a branch of a different name on Git如何在 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 (:).要推到一个不同的名称的一个分支,你只需要指定
branch you want to push
,你想推到用冒号分隔的分支名称(:)。
For example, if you want to push a branch called some-branch
to my-feature
:例如,如果要将名为
some-branch
推送到my-feature
:
(some-branch)$ git push origin some-branch:my-feature
Total 0 (delta 0), reused 0 (delta 0)
To github.com:johnmosesman/burner-repo.git
+ 728f0df...8bf04ea some-branch -> my-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:您不需要经常从本地推送所有分支,但如果您这样做,您可以添加
--all
标志:
(main)$ git branch
* main
my-feature
(main)$ git push --all
...
To github.com:johnmosesman/burner-repo.git
b7f661f..6e36148 main -> main
* [new branch] my-feature -> my-feature
If you want to:如果你想:
Then it's as simple as this:那么就这么简单:
git push origin HEAD:temp-branch-name
Note: You can replace HEAD
with any other branch or commit ID to push that instead.注意:您可以用任何其他分支或提交 ID 替换
HEAD
来推送它。
How can I push a local Git branch to a remote with a different name easily?
如何轻松地将本地 Git 分支推送到具有不同名称的远程?
Here is a short summary of just the key commands you need in general:以下是您通常需要的关键命令的简短摘要:
# push to a remote branch which has a different name
git push -u origin local_branch:remote_branch
# pull from a remote branch which has a different name
git pull origin remote_branch
# Set your upstream to something new in case you want to change it
git branch -u origin/remote_branch
# Unset your upstream
git branch --unset-upstream
# See what your upstream is currently set to
git branch -vv
There are too many incomplete and partial answers here which leave me with a lot of questions and a lot to be desired.这里有太多不完整和不完整的答案,这给我留下了很多问题和很多不足之处。 So, here is my attempt at providing a complete solution.
所以,这是我试图提供完整解决方案的尝试。
To push FROM local_branch
TO remote_branch
, you must specify both branches like this:要将FROM
local_branch
推local_branch
remote_branch
,您必须像这样指定两个分支:
git push origin local_branch:remote_branch
# General form
# NB: if you don't specify the `:remote_branch`, it is assumed to be the same
# name as `local_branch` on the `remote`! This means it pushes FROM local_branch
# TO local_branch, EVEN IF YOU DIDNT HAVE local_branch checked-out at the time!
# This can be incredibly confusing if you *thought* you had just told
# your currently-checked out branch to push to some branch on the remote
# and instead, the local copy of that some branch gets pushed to the remote.
git push <remote> <local_branch>[:remote_branch]
The documentation for this is hard to find, but it's actually found in the man git push
pages near the top under the "<refspec>...
" section:这方面的文档很难找到,但实际上可以在
"<refspec>...
”部分下靠近顶部的man git push
页面中找到:
The format of a
<refspec>
parameter is an optional plus+
, followed by the source object<src>
, followed by a colon:
, followed by the destination ref<dst>
.<refspec>
参数的格式是一个可选的加号+
,后跟源对象<src>
,后跟一个冒号:
,后跟目标引用<dst>
。
And then later:然后后来:
:<dst>
part can be omitted—such a push will update a ref that<src>
normally updates without any<refspec>
on the command line.:<dst>
部分可以省略——这样的推送将更新<src>
通常更新的引用,而命令行上没有任何<refspec>
。
This documentation is non-intuitive and not easy to understand at all without an example.该文档不直观,没有示例就根本不容易理解。
[BETTER] You can also set the upstream branch at the same time as pushing : [更好]您还可以在 push 的同时设置上游分支:
git push -u origin local_branch:remote_branch
# OR (same thing)
git push --set-upstream origin local_branch:remote_branch
# General form
git push -u <remote> <local_branch>[:remote_branch]
As part of the output of the command above, you should see:作为上述命令输出的一部分,您应该看到:
Branch 'local_branch' set up to track remote branch 'remote_branch' from 'origin'.
To make it obvious what is happening there, know that either of the two commands just above are equivalent to these two separate commands:为了使那里发生的事情一目了然,请知道上面的两个命令中的任何一个都等效于这两个单独的命令:
git push origin local_branch:remote_branch
git branch -u origin/remote_branch
Now, to view what your branch's upstream branch is currently set to , run the double-verbose ( -vv
) git branch
cmd:现在,要查看您的分支的上游分支当前设置的内容,请运行双详细(
-vv
) git branch
cmd:
git branch -vv
Sample output:示例输出:
Here you can see that the upstream branch is origin/master
, which means the master
branch on the remote named origin
:在这里你可以看到上游分支是
origin/master
,这意味着远程名为origin
的master
分支:
* master b2f0466 [origin/master] c/array_filter_and_remove_element.c: add O(n) in-place solution
Notes:笔记:
-vv
above means "double verbose". -vv
上面的意思是“双重冗长”。 This means it will print git branch
not just verbosely, but double verbosely, or extra verbosely.git branch
,而且会详细地加倍或更加详细地打印出git branch
。 The "extra verbose" part includes the upstream branch in square brackets, as shown above: [origin/matser]
. [origin/matser]
。git remote -v
.git remote -v
查看所有遥控器。 origin
is the remote shown in the examples above. origin
是上面示例中显示的遥控器。[Recommended if you already have branch local_branch
checked-out!] To pull FROM remote_branch
TO local_branch
, you must specify the remote branch to pull from, like this: [如果你已经有分支
local_branch
签出,推荐!]要从remote_branch
local_branch
,你必须指定要拉的远程分支,如下所示:
# THIS ASSUMES YOU ARE ALREADY CHECKED-OUT ON branch `local_branch`!
git pull origin remote_branch
# General form
git pull <remote> [remote_branch]
You can also specify both branches, but I'm not entirely sure what the difference is in this case (someone help me out here please if you know):您也可以指定两个分支,但我不完全确定在这种情况下有什么区别(如果您知道,请在这里帮助我):
git pull origin remote_branch:local_branch
[The following command only works if the remote and local branches have the same name! [以下命令仅适用于远程和本地分支名称相同的情况! (therefore it does NOT answer this Stack Overflow question), and is recommended if you do NOT already have branch
local_branch
checked-out!] [I use this command frequently too] To pull FROM a remote branch named some_branch
TO a local branch named some_branch
, while you do NOT have some_branch
locally checked-out , do this special form of git fetch
: (因此它不会回答这个堆栈溢出问题),并建议,如果你不已经有分支
local_branch
签出!] [我使用这个命令常常过于]拉名为远程分支some_branch
到本地分支命名some_branch
,虽然你没有在本地签出some_branch
,但执行这种特殊形式的git fetch
:
git fetch origin some_branch:some_branch
Notes:笔记:
git push
, git pull
does NOT have a -u
option.git push
不同, git pull
没有-u
选项。You can set your local branch named local_branch
to track an upstream branch named remote_branch
at the same time as pushing by using the git push -u
cmd shown above.你可以设置你的本地分支命名
local_branch
跟踪上游分支命名remote_branch
同时通过使用作为推动git push -u
以上CMD所示。
You can also set your local branch named local_branch
to track an upstream branch named remote_branch
like this:您还可以设置你的本地分支命名
local_branch
跟踪名为上游分支remote_branch
是这样的:
# Set local_branch to track origin/remote_branch (`remote_branch` on remote
# `origin`)
git branch --set-upstream-to=origin/remote_branch local_branch
# OR (same thing as just above)
git branch -u origin/remote_branch local_branch
# General form
git branch -u <remote>/<remote_branch> [local_branch]
# OR, same as above if the currently-checked-out branch is `local_branch`
git branch --set-upstream-to=origin/remote_branch
# OR (same thing as just above)
git branch -u origin/remote_branch
# General form
git branch -u <remote>/<remote_branch>
To UNset your upstream branch for local_branch
, so it no longer tracks the previously-set upstream branch, run this:要取消设置
local_branch
的上游分支,使其不再跟踪先前设置的上游分支,请运行以下命令:
git branch --unset-upstream local_branch
# OR, same as above if the currently-checked-out branch is `local_branch`
git branch --unset-upstream
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:再次,如上所示,要查看您的分支的上游分支当前设置的内容,请运行双详细(
-vv
) git branch
cmd:
git branch -vv
git push -u origin local_FROM_branch:remote_TO_branch
syntax: @Adam Dymitruk's answergit push -u origin local_FROM_branch:remote_TO_branch
语法的地方: @Adam Dymitruk 的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.