推送到 git 远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5082249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:10:12  来源:igfitidea点击:

pushing to git remote branch

gitgithub

提问by julio

I could use a hand with learning how to push a local branch to a remote branch. Please see below. Help much appreciated!

我可以动手学习如何将本地分支推送到远程分支。请参阅下文。非常感谢帮助!

The local branch was created after cloning the repo then doing

本地分支是在克隆 repo 然后做之后创建的

$ git checkout -b mybranch remotes/origin/mybranch

$ git branch -a
  master
* mybranch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/mybranch

But when trying to push changes back up:

但是当尝试将更改推回时:

$ git push mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

$ git push remotes/origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

$ git push origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

回答by markmc

As Abizern says, this works:

正如 Abilern 所说,这是有效的:

git push origin mybranch

But, to explain further, the mybranchpart is a refspec. This specifies the remote ref which should be updated with the given local commit.

但是,为了进一步解释,在mybranch部分是的Refspec。这指定了应该使用给定的本地提交更新的远程引用。

So, the command above is equivalent to:

所以,上面的命令等价于:

git push origin mybranch:mybranch

or even:

甚至:

git push origin mybranch:refs/heads/mybranch

and, indeed, since you're on the local mybranch, you could have done:

而且,事实上,由于您在本地 mybranch 上,您可以这样做:

git push origin HEAD:mybranch

This is good to understand, because I often find myself doing things like:

这很好理解,因为我经常发现自己在做以下事情:

git push origin HEAD^:mybranch

where you want to push all but the topmost patch to the remote branch.

要将除最顶层补丁之外的所有补丁推送到远程分支的位置。

Finally, if you want to delete the remote mybranch, you do:

最后,如果要删除远程 mybranch,请执行以下操作:

git push origin :mybranch

回答by Abizern

Try

尝试

git push origin mybranch

This pushes your branch named mybranchto the remote named origin

这会将名为mybranch的分支推送到远程命名

回答by MST

This is an old question, but I used this page as a ref back in the day, and have an answer with a different perspective. From my experience, the best way is to tweak your config settings such that a git pushis all that you will have to enter in the end.

这是一个老问题,但我在当天使用此页面作为参考,并从不同的角度给出了答案。根据我的经验,最好的方法是调整您的配置设置,git push以便您最终必须输入 a 。

You will push to the same remote branch that you update your code from:

您将推送到更新代码的同一个远程分支:

  • git config --global push.default upstream
  • git config --global push.default upstream

And now, you set the remote branch as upstream (if it wasn't already):

现在,您将远程分支设置为上游(如果尚未设置):

  • git branch --set-upstream-to origin/the_master
  • NOTE: Older versions can fall back upon this deprecated form of the command/.
  • git branch --set-upstream local_branch origin/the_master
  • git branch --set-upstream-to origin/the_master
  • 注意:旧版本可以使用这种弃用的 command/ 形式。
  • git branch --set-upstream local_branch origin/the_master

You have two branches - a local and a remote. Doing a git pullor git pushwithout args should, and will now, do what you want. This will not limit you to pushing to remote branches with the same name as the local one, as some of the above commands do.

你有两个分支——一个本地的和一个远程的。做git pullgit push不做 args 应该,现在,做你想做的。这不会像上述某些命令那样限制您推送到与本地分支同名的远程分支。

One final thing I usually do: modify git pullto change from this sequence:

我通常做的最后一件事:修改git pull以更改此序列:

  • git fetch && git merge [remote_upstream]to
  • git fetch && git rebase [remote_upstream]
  • git fetch && git merge [remote_upstream]
  • git fetch && git rebase [remote_upstream]

With this command: git config --global branch.autosetuprebase remote

使用此命令: git config --global branch.autosetuprebase remote