基于现有本地分支在存储库上创建远程分支

时间:2016-02-17 16:00:34

标签: git

我想将 origin master 推送到另一个远程存储库以及此远程存储库上的特定分支。问题是,这个分支在远程存储库上还不存在。如何在此存储库中创建远程分支?

基本上,我想做git push origin master:myRemote newBranchToCreate之类的事情,git remote add myRemote {remoteUrl}已经添加了 myRemote ,而 newBranchToCreate 尚未存在应该被创造。

4 个答案:

答案 0 :(得分:1)

推出已检出的主分支

如果您只想将当前的主分支推送到远程,语法很简单。例如:

# Make sure you're current with origin.
git checkout master
git pull

# Create a new remote, then push the current branch
# to a new branch on that new remote.
git add myNewRemote <url>
git push myNewRemote master:someNewBranch

推送上游 Origin / Master

如果要推送当前上游分支中的内容而不是已签出的本地主分支,可以使用隐藏的 origin / master 分支以及新远程上的完全限定refspec 。要将非当前分支推送到新远程数据库上不同名称的分支:

# Make sure you have the latest version of origin/master
# in your repository.
git fetch --all

# Create the new remote and push origin/master
# to an arbitrary branch on that remote.
git add myNewRemote <url>
git push myNewRemote origin/master:refs/remotes/NewRemote/someNewBranch

答案 1 :(得分:1)

您可以使用“git push myRemote master:newBranchToCreate”

答案 2 :(得分:1)

  

我想将我的origin master推送到另一个远程存储库以及此远程存储库上的特定分支

为了做到这一点,你必须创建一个新分支,然后推送它。 (假设您使用的是git版本&gt; 2.0。

为什么?

因为在git 2.0版中,pull和push的机制被更新了

  

git push [$there]没有说要推送什么时,我们已经使用了   到目前为止传统的 匹配 语义(所有分支都已发送   只要已经存在同名分支,就可以到远程控制台   在那边)。在Git 2.0中,默认为 简单 语义,   推动:

     
      
  • 只有当前分支到同名的分支,并且只有   当前分支设置为与该远程集成时   分支,如果你正在推送到同一个遥控器;或

  •   
  • 只有当前分支到具有相同名称的分支,如果您   正在推送到一个不是你常去的地方的遥控器。

  •   
     

您可以使用配置变量push.default进行更改   这个。如果你是一个想要继续使用的老人   matching语义,您可以将变量设置为matching   例。阅读文档以了解其他可能性。

怎么做?

# update your local repository
git fetch --all --prune

# checkout the desired branch you want to start from 
git checkout <branch_name>

# grab the last changes into the branch
git pull origin <branch name>

# now create the new branch name you want to push
git checkout -b <new branch>

# Now you can push the branch with the new name to your remote.
# The explantion why is in the previous section

答案 3 :(得分:0)

Do this with ordinary push. Git supplies a lot of defaults for the usual cases, but the fundamental push operation is a repository url or path, a local commit1, and a remote refname。你没有提供任何东西,git会为你寻找。

您可以使用任何存储库测试此内容。任何提交。任何refname。

git init ../fun
git push ../fun HEAD:refs/heads/funfunfun

其他一切只是设置git的机器来为你提供默认值。想要一个方便的名称来引用其他存储库以防它发生变化,或者你可以将一些默认值和其他注释与之关联起来吗?设置“远程”。等等。

git push docs

git remote docs

git config docs

修改:专门做

  

基本上,我想做git push origin master:myRemote newBranchToCreate

之类的事情

你所描述的,如果我正确地解释它,那么命令就是

git push myRemote refs/remotes/origin/master:refs/heads/newBranchToCreate

并且你可能会origin/master refs/remotes/origin/master,因为如果一个refname不存在,git会检查一些看起来很可能的快捷方式是否有效,但请记住,所有内容都不完整-spell命令依靠启发式搜索通过你的repo试图找出你可能意味着什么。这是有限制的。

1 它不一定是提交。