【问题标题】:How to create a "master" branch in a Git repo after an initial commit has been made?初始提交后如何在 Git 存储库中创建“主”分支?
【发布时间】:2020-12-03 04:40:01
【问题描述】:

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

  • 有一个master 分支(例如,指向添加一个空的README 文件的提交)
  • 我的更改中有一个功能分支,可以用来创建 PR 吗?

任何帮助将不胜感激。

【问题讨论】:

  • 你的意思是你正在使用两个分支?然后将你的工作分支合并到本地 repo 上的 master 分支,然后 git push 你的 github repo
  • 不,我在本地没有master,只是一个带有单个提交的功能分支,这是存储库中的初始提交。

标签: git github branch commit


【解决方案1】:

根据您的问题,您的存储库现在如下所示:

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

现在,创建master 分支,指向提交之前 feature 引用的那个:

git checkout -b master feature^

此时,您只需将master 推送到 GitHub 即可:

git push -u origin master

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 2012-06-03
      • 1970-01-01
      • 2020-09-23
      • 2013-09-23
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2019-07-17
      相关资源
      最近更新 更多