首页 > 解决方案 > 将 master 重置为空状态

问题描述

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

因此,我想:

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

标签: ruby-on-railsgitgithubresetmaster

解决方案


从 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

推荐阅读