【问题标题】:Reset master to an empty state将 master 重置为空状态
【发布时间】:2019-10-18 23:58:43
【问题描述】:
我创建了一个新的 Rails 应用程序并将代码直接从 master 推送到 Github(第一次提交到存储库中)。但是我犯了一个错误,我不想直接从 master 提交这个新的 Rails 应用程序,而是从 master 创建一个新分支并从这个新分支推送新的 Rails 应用程序。
因此,我想:
- 在 Github(remote) 中从 master 中删除提交,所以 master 是 EMPTY
- 从 master 创建一个新分支,并将 master 中的先前提交添加到这个新分支中。
- 将其推送到 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】:
您还可以对您的存储库尝试以下步骤:
-
git checkout master: 确保你是master。
-
git checkout -b my-fancy-new-branch:创建您的新功能分支。
-
git checkout master:切换回master
-
git reset --hard rootcommit:将 master 重置为您自己提交之前的状态。
- 可选,如果你有一个遥控器,你可以从:
git pull --ff(如果因为拉动不是快进而失败,你必须重新考虑rootcommit。它包含你的一些工作)
我尝试在我的测试存储库上执行此操作,它似乎有效。
我从这个answer 中获得了参考,这也有助于找到其他方法。