你如何从功能分支撤消对主线的改变,但保持分支是最新的? [英] How do you undo changes committed to main line from a feature branch but keep the branch up to date?

查看:105
本文介绍了你如何从功能分支撤消对主线的改变,但保持分支是最新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我们会遇到一些问题,我们会将特性分支提交给我们的基线,并发现在某些情况下它可能会出现可怕的错误,因此需要恢复更改,而不会丢失自那时起完成的所有其他提交。但是,有人需要继续在原分支上工作。



我不想使用 git rebase 改变历史的主线(因为它本身会导致各种各样的问题),但是当我在主线上执行 git revert 时,只要<$在分支上运行c $ c> git merge ,以完成对系统做的任何其他更改,然后完成的所有工作都消失。



我能做些什么来保持分支上的变化不变?

解决方案

我假设你不想做任何事情如果您有两个分支 master 和<$ c $,那么这是仅合并方法。

C>特征。
因此,您已决定将该功能合并到 master 中:

   

现在,您拥有了所有特性主人的变化。我假设最困难的情况 - 在两个分支合并之后都有提交。但是,突然你发现功能没有任何功能,所有功能都被破坏并决定恢复它。您的历史记录是(您可以在 git log --graph master master 中看到它):

  * commit 838f29fda2333bdbfc0d1be44d2387e6119dc4e8 
|
| f3
|
| * commit 58d1e3f0e032cc77d4ab2bbf0bdfe6407b96b7e6
| |
| | m3
| |
| * commit afb600fe382befe095f9b8a6e9eef98be25c929b
| | \ Merge:5cddc52 8660a59
| | /
| / |
| |合并分支'功能'
| |
* |提交8660a59eeb13f9b3447846ba6977140f7c86c7a4
| |
| | f2
| |
* |提交6f76e2288d69808b97981340198fd8044ab47630
| |
| | f1
| |
| * commit 5cddc528de424a01d1d18552ddea93435618e2f7
| |
| | m2
| |
| *提交fa6ebb76f9f0308f7dcd1148503534668fa15358
| /
|
| m1
|
* commit 4e3aa05d6f46eb14bbbc253a42325e9a53a3393e

注意合并后的提交f3和m3。因此,您需要谨慎删除 master 中出现的 feature 中的变化。



要做到这一点,您可以在 master 上恢复合并提交,并保留master的基础(参数 -m1 )。为了实现它,我创建了一个基于合并点的新分支并恢复它:

  $ git checkout -b特征恢复afb600fe 
$ git revert -m1 HEAD

现在,我们在分支上还原了更改功能复归。现在很简单,只需通过合并就可以将其放入主文件:

  $ git checkout master 
$ git merge feature-revert

所以,目前 master doesn没有任何来自功能的任何内容。但是,如果稍后想要重新合并该功能,则所有还原的更改都将在master中丢失,因为它们已合并并恢复。



要解决此问题,您应该恢复回复(he-he)并将其合并回<功能:

  $ git checkout feature-revert 
$ git revert HEAD
$ git checkout feature
$ git merge merge -revert

我想我们不再需要这个分支了,因为我们已经合并了它,但是如果你愿意,你可以保留它:

  $ git分支-d功能恢复

您应该知道,将它合并到 master > feature 这通常是正常的工作流程。

现在,在特性中进行修复后,您可以再次将它合并到 master 随时照常。


From time to time we can run across issues where we commit a feature branch to our baseline and discover in some situations it can go horribly wrong, so the change needs to be reverted, without losing all the other commits done since then. However, someone needs to continue working on the original branch.

I don't want to use git rebase on the main line to change history (because that'd cause all sorts of issues all by itself), but when I do git revert on the main line then as soon as git merge is run on the branch, to pick up any other changes done to the system, then all the work done disappears.

What can I do to keep the changes on the branch intact?

解决方案

I assume you don't want to do any rebases and resets, this is merge-only approach.

Suppose you have two branches master and feature. So, you've decided merge the feature into master:

$ git merge feature

Now, you have all your feature changes on the master. I assume the most difficult scenario - there are commits made after the merge on both branches. But, suddenly you see that nothing works of feature, all is broken and decide to revert it. Your history is (you could see it with git log --graph feature master):

* commit 838f29fda2333bdbfc0d1be44d2387e6119dc4e8
| 
|     f3
|    
| * commit 58d1e3f0e032cc77d4ab2bbf0bdfe6407b96b7e6
| | 
| |     m3
| |     
| *   commit afb600fe382befe095f9b8a6e9eef98be25c929b
| |\  Merge: 5cddc52 8660a59
| |/  
|/|   
| |       Merge branch 'feature'
| |   
* | commit 8660a59eeb13f9b3447846ba6977140f7c86c7a4
| | 
| |     f2
| |   
* | commit 6f76e2288d69808b97981340198fd8044ab47630
| | 
| |     f1
| |   
| * commit 5cddc528de424a01d1d18552ddea93435618e2f7
| | 
| |     m2
| |   
| * commit fa6ebb76f9f0308f7dcd1148503534668fa15358
|/  
|   
|       m1
|  
* commit 4e3aa05d6f46eb14bbbc253a42325e9a53a3393e

Notice the commits f3 and m3 which have been done after merge. So, you need carefully delete only changes from feature appeared in the master.

To do it you could revert the merge commit on the master, keeping master's base (the parameter -m1). To achieve it I create a new branch based on the merge point and revert in it:

$ git checkout -b feature-revert afb600fe
$ git revert -m1 HEAD

Now, we have a reverted changes on the branch feature-revert. Now it is simple just put it into master by merging:

$ git checkout master
$ git merge feature-revert

So, at the moment the master doesn't have anything from the feature. However, if later you want to re-merge the feature, all reverted changes will be lost in master, as they were merged and reverted.

To solve the issue you should revert the revert (he-he) and merge it back to the feature:

$ git checkout feature-revert
$ git revert HEAD
$ git checkout feature
$ git merge feature-revert

I suppose we don't need the branch anymore, as we merged it already, but you could keep it if you want:

$ git branch -d feature-revert

You should be aware, that merging it back brings all changes from master into feature which usually normal workflow.

Now, after fixes made in feature you could merge it again into master as usual anytime.

这篇关于你如何从功能分支撤消对主线的改变,但保持分支是最新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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