如何在没有整个分支的情况下仅对最后两个提交进行重新设置? [英] How to rebase only last two commits without the whole branch?

查看:49
本文介绍了如何在没有整个分支的情况下仅对最后两个提交进行重新设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心犯了一个错误的分支.该图看起来像这样

I accidentally committed to a wrong branch. The graph looks like this

----o--o-----o (master)
     \
      \
       A--B--C--D (feature)

我想在不更改A和B的情况下将C和D移至母版.我希望图形看起来像这样

I want to move C and D to master without the changes in A and B. I want the graph to look like this

----o--o-----o (master)
     \        \
      \        C--D (other_feature)
       \
        A--B (feature)

这可能吗?以及如何在远程进行呢?

is this possible? and how to do it also on remote?

A和B的更改不会合并,所以我不在乎提交是否会丢失

Changes in A and B are not going to be merged so I don't care if the commits will be lost

推荐答案

警告:以下git命令将重写feature分支的历史记录. git rebasegit reset是危险的,因为它会对使用feature分支的其他任何人造成严重破坏.

Warning: The following git commands will rewrite the history of the feature branch. git rebase and git reset are dangerous because it can wreak havoc for anyone else who is using the feature branch.

首先在与feature相同的提交中创建分支other_feature.

First create the branch other_feature at the same commit as feature.

git checkout -b other_feature feature

将前两次提交重新设置为master.

Rebase the previous two commits onto master.

git rebase --onto master HEAD~2

结帐feature.

git checkout feature

feature重置为所需的提交.

git reset --hard HEAD~2

这篇关于如何在没有整个分支的情况下仅对最后两个提交进行重新设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆