的提交移动到新的分支

的提交移动到新的分支

本文介绍了如何将最近(但不是最新)的提交移动到新的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即。我怎样才能从

 

master:ABCDEFG

 
主人:ABEFG
\
功能:CD

显然现在已经清楚CD应该已经在一个功能分支中,因为这个改变现在已经被延迟了。

这与我见过的其他人存在微妙差异和

解决方案

这个答案假定你没有推动 master 或者你是唯一从事该项目的人(在这种情况下,这并不重要)。如果您推送了 master 并且其他人拉取了您的更改,则需要与他们进行交流,以确保他们正确更新存储库。

 总而言之,你应该可以做到这一点。

  git branch< feature> < d取代; #创建一个名为< feature>的分支在提交时< D> 
git还原< D> #撤消D
git还原< C> #撤销C

这会产生类似这样的历史记录

  ABCDEFG-!D-!C 
^ ^
功能大师

任何检出 master 的人都会看到代码,就像从未发生过提交 C D ,你只需要恢复在你的历史中。这是支持已推送分支的更改的首选方式。


i.e. how do I go from


master : A-B-C-D-E-F-G

to

master: A-B-E-F-G
           \
feature:    C-D

Obviously its now clear that CD should have been in a feature branch, as that change is now delayed.

This is a subtly different issue to the others I've seen here and and here

解决方案

This answer assumes you either haven't pushed master or you're the only person working on the project (in which case it doesn't really matter). If you have pushed master and someone else pulled your changes, you'll need to communicate with them to make sure they update their repositories appropriately.

                                  # Assuming you're on master
git branch <feature> <D>          # Create a branch named <feature> at commit <D>
git rebase --onto <B> <D> master  # Transplant branch

Git rebase documentation has a pretty good graphical explanation of what rebase --onto does.


If you want to avoid a rebase altogether, you should be able to do

git branch <feature> <D>          # Create a branch named <feature> at commit <D>
git revert <D>                    # undo D
git revert <C>                    # undo C

This will result in a history like this

A-B-C-D-E-F-G-!D-!C
      ^           ^
   feature      master

Anyone who checks out master, will see code as if commits C and D never happened, you'll just have a couple of reverts in your history. That's the preferred way of backing changes out of a branch that has been pushed.

这篇关于如何将最近(但不是最新)的提交移动到新的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:24