简体   繁体   English

推送到git远程分支

[英]pushing to git remote branch

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

As Abizern says, this works: 正如Abizern所说,这有效:

git push origin mybranch

But, to explain further, the mybranch part is a refspec . 但是,为了进一步解释,在mybranch部分是的Refspec。 This specifies the remote ref which should be updated with the given local commit. 这指定了应该使用给定的本地提交更新的远程引用。

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

Try 尝试

git push origin mybranch

This pushes your branch named mybranch to the remote named origin 这会将名为mybranch的分支推送到远程命名

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 push is all that you will have to enter in the end. 根据我的经验,最好的方法是调整你的配置设置,这样就可以在最后输入一个git push

You will push to the same remote branch that you update your code from: 您将推送到更新代码的同一远程分支:

  • 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

You have two branches - a local and a remote. 你有两个分支 - 本地和远程。 Doing a git pull or git push without args should, and will now, do what you want. 做一个没有args的git pullgit push现在应该做你想做的事。 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. 这不会限制您推送到与本地名称相同的远程分支,就像上面的一些命令那样。

One final thing I usually do: modify git pull to change from this sequence: 我通常做的最后一件事:修改git pull以改变这个序列:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM