如何在完成初始提交后在 Git 存储库中创建 "master" 分支?

How to create a "master" branch in a Git repo after an initial commit has been made?

我在 empty Git 存储库中创建了一个功能分支并将其推送到 Github。现在我无法从它创建 PR,因为它被认为是“默认”分支并且没有 master 分支。如何更新存储库以便:

如有任何帮助,我们将不胜感激。

如果你的仓库只有一个特性分支,并且那个分支只有一个提交,而 GitHub 中的仓库没有提交,只需将你的特性分支重命名为 master 并推送它。无需拉取请求。

一切都必须从某个地方开始。首次提交到新存储库不需要拉取请求。拉取请求需要两个分支,以便您可以查看引入的更改。由于这是一个新的存储库,因此没有新的更改。

根据您的问题,您的存储库现在看起来像这样:

o
^
feature

你想让它看起来像这样:

o-------o
^       ^
master  feature

同时还使 master 成为新的 default branch on GitHub,而不是 feature

一种方法是在 feature 分支上创建一个新的“初始”提交,将其移动到历史的开头并创建一个新的 master 分支指向它。

步骤如下:

git checkout feature
git commit --allow-empty -m "Initial commit" (you can create the README file here, instead)
git rebase -i --root

# The TODO file will look something like this:

pick 1234abc Adds the feature
pick 5678edg Initial commit

# Move the "Initial commit" line to the top of the file

pick 5678edg Initial commit
pick 1234abc Adds the feature

# Then save and close

此时,您的历史记录将如下所示:

o-------o
        ^
        feature

现在,在 feature:

引用的 之前创建指向提交 master 分支
git checkout -b master feature^

在这一点上,你所要做的就是将master推到GitHub:

git push -u origin master

最后,您必须转到 GitHub 上的存储库设置,使 master 成为新的默认分支。请参阅 GitHub 关于 how to do that 的文档。