【问题标题】:Rollback two commits. Reapply second commit. Branch and apply first commit回滚两个提交。重新应用第二次提交。分支并应用首次提交
【发布时间】:2016-01-24 14:15:29
【问题描述】:

在我的项目中,我最近做了两次提交。提交在不同的文件集上。我还远程推送了这些提交。类似于以下内容-

git commit file-a file-b -m "first commit of new features"
git commit file-c file-d -m "second commit of new features"
git push -u origin master

我想做以下事情-

  1. 将 master 回滚到第一次提交之前,然后应用 第二次提交。
  2. 此时创建功能分支 并应用第二次提交(它也应该有第一次提交)。

这可以通过简单的方式实现吗?我考虑过使用 git diff 创建和使用补丁文件,但我想我会先检查是否有更好的方法。

【问题讨论】:

  • 阅读有关交互式变基和樱桃采摘的信息。

标签: git git-branch git-commit


【解决方案1】:

此时创建一​​个功能分支并应用第二次提交(它也应该有第一次提交)。

只需在您的 master 当前所在的位置创建一个功能分支:

git branch feature_branch

将 master 回滚到第一次提交之前,然后仅应用第二次提交。

git reset --hard @~2

在功能分支上重新排序提交

git checkout feature_branch
git rebase -i master
# switch second and first commit order

然后将 master 重置为 feature_branch~1(这是第二次提交)

git checkout master
git reset --hard feature_branch~1

最后,推动一切

git push --force origin master
git push -u origin feature_branch

【讨论】:

  • 完美运行,谢谢。我用git rebase -i HEAD~2切换顺序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 2017-07-27
  • 2017-03-17
  • 2012-08-31
  • 1970-01-01
相关资源
最近更新 更多