git:如何移动分支的根目录提交回来 [英] git: how to move a branch's root two commits back

查看:84
本文介绍了git:如何移动分支的根目录提交回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

  A  -  B  -  C  -  D  -  E  -  F master 
\\'
\ -G -H新功能分支

现在我意识到提交B和C实际上
属于新功能,所以我想将它们移动到新功能分支。换句话说,我希望新功能分支从A开始,并包含提交B和C:

  A  - D  -  E  -  F master 
\
\ - B - C - G - H新功能分支

我该怎么做?从我读过的内容来看,似乎 rebase 是我正在寻找的功能,但是在我搞乱我的存储库之前,我想确定一下。



(我已经搜索并发现了很多类似的问题和示例,但没有一个完全像 所以我要求确定(毕竟,回购是毁灭的一件宝贵的事情))。

修复master:

  git checkout A 
git cherry-pick master〜3..master#实际上想要继续掌握
git分支-f master#重新定位master在新位置
git checkout master

从特性分支中删除提交D:

  git checkout feature-branch〜3#或git checkout C 
git cherry-pick功能分支〜2..feature-branch#应用最后2个修订
git分支-f功能分支
git结账功能分支


Let's say I have:

A - B - C - D - E - F  master
            \ 
             \- G - H  new feature branch

Now I realize that commits B and C actually belong to the new feature, so I want to move them to the "new feature branch". In other words, I want the "new feature branch" to start at A, and include commits B and C:

A - D - E - F  master
 \ 
  \- B - C - G - H  new feature branch

How do I do this? From what I've read, it seems that rebase is the feature I'm looking for, but I'd like to be sure before I mess up my repository.

(I've searched and found lots of questions and examples that are very similar, but none that are exactly like the scenario I've described, so I'm asking to be sure (a repo is a precious thing to ruin, after all)).

解决方案

To fix master:

git checkout A
git cherry-pick master~3..master # apply the changes we actually want to keep on master
git branch -f master # reposition master on new position
git checkout master

To drop commit D from the feature branch:

git checkout feature-branch~3 # or git checkout C
git cherry-pick feature-branch~2..feature-branch # apply the last 2 revisions
git branch -f feature-branch
git checkout feature-branch

这篇关于git:如何移动分支的根目录提交回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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