git Github 创建空分支

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

Github create empty branch

gitgithub

提问by Sebastian Schneider

I'm searching in the internet for hours and can't find a solution for this problem:

我在互联网上搜索了几个小时,但找不到解决此问题的方法:

I want to create a new GitHubbranch, called release. This branch needs to be empty! However there is a existing branch with x commits and i don't want to have its commit history.

我想创建一个GitHub名为release. 这个分支必须是空的!但是,有一个带有 x 提交的现有分支,我不想将其commit history.

The only method I found is to create a local--orphanbranch

我发现的唯一方法是创建一个本地--orphan分支

回答by C. Augusto Proiete

What's wrong with the --orphanoption? If you want a branch that is empty and have no history, this is the way to go...

--orphan选项有什么问题?如果你想要一个空的并且没有历史的分支,这是要走的路......

git checkout --orphan empty-branch

Then you can remove all the files you'll have in the staging area (so that they don't get committed):

然后,您可以删除暂存区域中的所有文件(这样它们就不会被提交):

git rm -rf .

At this point you have an empty branch, on your machine.

此时,您的机器上有一个空分支。

Before you can push to GitHub (or any other Git repository), you will need at least one commit, even if it does not have any content on it (i.e. empty commit), as you cannot push an empty branch

在您可以推送到 GitHub(或任何其他 Git 存储库)之前,您至少需要一次提交,即使它上面没有任何内容(即空提交),因为您不能推送空分支

git commit --allow-empty -m "root commit"

Finally, push it to the remote, and crack open a beer

最后推到遥控器,开一瓶啤酒

git push origin empty-branch

回答by thisismydesign

--orphanis good for creating an empty branch locally, however in order to push it or interact with other branches you will need a commit.

--orphan适合在本地创建一个空分支,但是为了推送它或与其他分支交互,您需要提交。

Creating a new commit on your orphan branch it is not a good idea because you won't be able to interact with your other branches. I.e.

在孤立分支上创建新提交不是一个好主意,因为您将无法与其他分支进行交互。IE

git checkout --orphan test
git commit --allow-empty -m "init test branch"
git merge master
fatal: refusing to merge unrelated histories

Instead, you should prefer creating a new branch from the first commit of master. If the commit is not empty you can add an empty commit before the first one, as explained by @houtanb.

相反,您应该更喜欢从 master 的第一次提交创建一个新分支。如果提交不为空,您可以在第一个提交之前添加一个空提交,如@houtanb 所述。

回答by houtanb

You can also follow the instructions hereto create an empty commit at the root of your masterbranch. Then just create your releasebranch where that empty root commit is.

您还可以按照此处的说明master分支的根目录创建一个空提交。然后只需release在空的根提交所在的位置创建您的分支。

回答by Greg M. Krsak

The accepted answer led me to some problems, so I did this:

接受的答案让我遇到了一些问题,所以我这样做了:

$ git branch
* staging
$ git branch master c74d99cf46f6ed23e742f2617e9908294b4a608b
$ git checkout master
Switched to branch 'master'

And got what I wanted without and merge / pull-request issues. I just had to pick a base commit to create my second branchfrom.

并在没有合并/拉取请求问题的情况下得到了我想要的。我只需要选择一个基本提交来创建我的第二个分支