简体   繁体   English

是否可以在 git 中创建一个新的、空的远程分支而无需推送?

[英]Is it possible in git to create a new, empty remote branch without pushing?

Most examples of creating remote branches involve pushing from a local branch创建远程分支的大多数示例都涉及从本地分支推送

Is there a way of creating an empty remote branch without pushing?有没有办法在不推送的情况下创建一个空的远程分支?

Is it also possible to create a local empty branch,check it out then link it to the new also empty remote branch without pushing?是否也可以创建一个本地空分支,检查它然后将它链接到新的也是空的远程分支而不推送?

As mentioned in the blog post " Start a New Branch on your Remote Git Repository ":正如博客文章“ 在远程 Git 存储库上启动新分支”中所述:

  • Creating a Remote Branch创建远程分支
git push origin origin:refs/heads/new_feature_name
  • Make sure everything is up-to-date确保一切都是最新的
git fetch origin
  • Then you can see that the branch is created.然后就可以看到分支创建完成了。
git branch -r

This should show ' origin/new_feature_name '这应该显示“ origin/new_feature_name

  • Start tracking the new branch开始跟踪新分支
git checkout --track -b new_feature_name origin/new_feature_name

So to declare a remote branch, even one which doesn't yet exist on the local repository, git push is mandatory.因此,要声明远程分支,即使是本地存储库中尚不存在的分支,也必须使用git push

git checkout --orphan new-empty-branch

Then然后

git rm -rf .

to remove all files from new branch.从新分支中删除所有文件。

It's worth pointing out that there is no such thing as an empty branch in Git.值得指出的是,Git 中没有空分支这样的东西。 A branch name, in Git, always selects some particular commit. Git 中的分支名称总是选择某个特定的提交。 That commit is, by definition, the last commit on that branch.根据定义,该提交是该分支上的最后一次提交。 So a branch always has at least one commit on it.所以一个分支总是至少有一个提交。

What's unusual about Git, compared to other version control systems at least, is that the commit that is the last commit on any one particular branch, can also be on some other branch(es) at the same time.至少与其他版本控制系统相比,Git 的不寻常之处在于,作为任何一个特定分支上的最后一次提交的提交,也可以同时在其他一些分支上。 It can be the last commit on more than one other branch, and it can be a non-last commit on some other branch(es).它可以是多个其他分支上的最后一次提交,也可以是其他某个分支上的非最后一次提交。

VonC's answer is usually the right one for those who are asking this question.对于问这个问题的人来说, VonC 的回答通常是正确的。

Onlyjob's answer is the right way to set things up to create a new branch that's not empty but that does not share any commits with any other existing branches. Onlyjob 的答案是正确的方法来设置创建一个新分支,该分支不为但不与任何其他现有分支共享任何提交。 That's a rare thing to want to do, but when that is what you want, creating a new branch name some other way won't work.这是一件罕见的事情,但是当您想要这样做时,以其他方式创建新的分支名称将不起作用。

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

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