在Git中,是否有可能以不改变本地文件的方式复制远程分支?如果没有,为实现这一目标必须做的最小变化是什么?
通过说不更改本地文件,我的意思是在整个过程中根本不更改它们。存放它们并在以后带回来被视为一种变化。 Checkout显然正在更改本地文件,它们将更改以反映您要检出的分支的状态。
示例,分支之前:
分支后:
答案 0 :(得分:3)
git branch
命令将允许您创建新分支,而无需执行任何签出或更改本地文件。然后你需要推动新的远程分支。
您需要执行的具体步骤:
git branch master-copy origin/master
- 创建您将在新远程git push origin master-copy
- 将本地分支推送到远程git branch -D master-copy
- 删除本地分支副本所有这一切都是在没有检查文件的情况下完成的。
答案 1 :(得分:2)
您需要做的是简单地创建一个指向提交1的远程分支。
简短回答
git push origin [commit1 hash]:refs/heads/master-copy
答案很长
让我们说这是你的存储库:
commit 2 (65bc341) O <-- master
|
(more commits) ...
|
commit 1 (125afe4) O <-- origin/master
要实现您想要的使用git push命令,您不需要创建本地分支。您需要将三个参数传递给push命令:
git push [REMOTE NAME] [WHERE SHOULD THE REMOTE BRANCH POINT TO]:[REMOTE BRANCH NAME]
假设您遵循标准名称,REMOTE NAME可能是“原产地”。
REMOTE BRANCH NAME是主副本。
最后,REMORE BRANCH POINT TO应该是commit1的哈希值(我上面使用的例子中的125afe4)
...所以命令是:
git push origin 125afe4:master-copy
这个命令你告诉git你在名为“origin”的远程控件中想要创建一个名为“master-copy”的新分支,指向提交125afe4。
如果您运行此命令(并且您没有创建任何名为master-copy的本地分支),您将收到此错误
error: unable to push to unqualified destination: master-copy
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@WHATEREVER_YOUR_REPO_URL_IS'
为什么你得到这个回复?因为git不知道您是否尝试在远程存储库中创建分支或标记。要解决你运行的歧义
git push origin 125afe4:refs/heads/master-copy
et voila!
commit 2 (65bc341) O <-- master
|
(more commits) ...
|
commit 1 (125afe4) O <-- origin/master origin/master-copy
如果您按照接受的答案中的步骤操作,您创建并稍后删除的本地分支“主副本”将解决歧义。我希望我可以为您提供有关git-push
如何工作的一些见解,并向您展示您并不真正需要此本地分支。您可以使用一个命令而不是三个命令来获得所需的内容!
请记住,如果您想稍后删除远程分支,则可以运行
git push --delete origin master-copy
或者您可以使用旧语法
git push origin :master-copy
答案 2 :(得分:0)
.1在提交1获取origin/master
的本地副本
git checkout -b master-copy master
.2将此新分支推送到原点
git push origin master-copy
.3在提交1更新origin/master
git push origin master-copy:master -f
.4删除本地master-copy
git branch -D master-copy
答案 3 :(得分:0)
打开终端
cd "master's project routes"
git branch master-copy
git checkout master-copy
git push origin master-copy
cmd + N
之后,您将在新分支中获得主项目的副本&#34; master-copy&#34;。如果您希望将来将其与主分支合并。我建议你使用feature / master-copy而不是master-copy。