【问题标题】:Reset master to an empty state将 master 重置为空状态
【发布时间】:2019-10-18 23:58:43
【问题描述】:

我创建了一个新的 Rails 应用程序并将代码直接从 master 推送到 Github(第一次提交到存储库中)。但是我犯了一个错误,我不想直接从 master 提交这个新的 Rails 应用程序,而是从 master 创建一个新分支并从这个新分支推送新的 Rails 应用程序。

因此,我想:

  1. 在 Github(remote) 中从 master 中删除提交,所以 master 是 EMPTY
  2. 从 master 创建一个新分支,并将 master 中的先前提交添加到这个新分支中。
  3. 将其推送到 Github。

【问题讨论】:

标签: ruby-on-rails git github reset master


【解决方案1】:

在 Github(remote) 中从 master 中删除提交,所以 master 是 EMPTY

您可以创建一个孤立分支 - 孤立分支是没有任何历史记录的分支

# Create "clean" branch
git checkout --orphan <name>

# remove all existing content if you wish
git clean -Xdf && git clean -xdf

从 master 创建一个新分支,并将 master 中的先前提交添加到这个新分支中。

几个选项:

# Option 1 - Start your branch from the last desired commit
git checkout -b <name> <SHA-1> 

# Option 2 - Set your branch to the desired commit 
git reset <SHA-1> --hard

# Add the required commit on top of your branch
git cherry-pick <SHA-1>

将其推送到 Github。

# force the update of the new branch
git push <origin> master -f

【讨论】:

    【解决方案2】:

    请使用以下命令从master创建一个新分支,

    git checkout -b branch_name
    

    结帐后进入您的新分支并将其推送到 Github。

    现在转到 master 分支并 remove the last 提交并推送到 Github

    【讨论】:

      【解决方案3】:

      您还可以对您的存储库尝试以下步骤:

      1. git checkout master: 确保你是master。
      2. git checkout -b my-fancy-new-branch:创建您的新功能分支。
      3. git checkout master:切换回master
      4. git reset --hard rootcommit:将 master 重置为您自己提交之前的状态。
      5. 可选,如果你有一个遥控器,你可以从:git pull --ff(如果因为拉动不是快进而失败,你必须重新考虑rootcommit。它包含你的一些工作)

      我尝试在我的测试存储库上执行此操作,它似乎有效。

      我从这个answer 中获得了参考,这也有助于找到其他方法。

      【讨论】:

        猜你喜欢
        • 2017-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 2021-01-15
        • 2020-11-15
        • 2020-04-12
        • 2020-11-23
        相关资源
        最近更新 更多