简体   繁体   English

GIT:如何重新设置基础,以便在功能分支中保留最新提交

[英]GIT: how to rebase so that the latest commit is preserved in feature branch

Newly transitioned from svn to the world of GIT. 从SVN到GIT世界的新过渡。

I have my master branch: A(initial commit) - B - C - D - E 我有我的主分支: A(初始提交)-B-C-D-E

My feature branch: A(initial commit) - B - Z 我的功能分支: A(初始提交)-B-Z

I want to rebase my feature branch to master such that: ABCDEZ 我想将我的功能分支重新设置为母版,例如: ABCDEZ

How could this be possible? 这怎么可能呢? I try to checkout to feature branch and do git rebase master, but apparently it gives me lot conflicts and newly added files of feature getting deleted. 我尝试签出到功能分支并执行git rebase master,但是显然这给了我很多冲突,并且新添加的功能文件被删除了。 I understand that it is rebasing like A - B - Z - C - D - E 我知道它像A-B-Z-C-D-E

Following saved my day (thanks to comment from @mrek. His advice gave the direction): 以下是我的一天(感谢@mrek的评论。他的建议提供了指导):

git checkout featurebranch git checkout功能分支

git rebase --onto masterbranch featurebranch^ git rebase-进入masterbranch featurebranch ^

(^ denotes parent commit of featurebranch referenced with featurebranch^) (^表示由featurebranch ^引用的featurebranch的父提交)

For more information: git rebase --onto explained 有关更多信息: git rebase --onto解释

This will do exactly what you need, assuming your feature branch was created from master: 假设您的功能分支是从master创建的,这将完全满足您的需求:

git checkout feature
git rebase master
# fix possible conflicts
git checkout master
git merge feature

Firstly, git rebase master on your feature branch will get Z and move it to a temporary space. 首先,功能分支上的git rebase master将获得Z并将其移动到临时空间。 Then bring C , D and E to feature and finally apply Z from the temporary area on top of E . 然后使CDE成为feature ,最后从E顶部的临时区域应用Z

Resulting in ABCDEZ 导致ABCDEZ

At this point, if there is any conflicts you could simply run git mergetool , solve the conflicts and then git rebase --continue . 在这一点上,如果有任何冲突,您可以简单地运行git mergetool ,解决冲突,然后git rebase --continue

If you run into any issues while fixing conflicts you can cancel, run git rebase --abort and try again. 如果在解决冲突时遇到任何问题可以取消,请运行git rebase --abort试。

After this, the subsequent merge back to master will be a fast-forward (no conflicts). 在此之后,随后的merge回主节点将是一个fast-forward (无冲突)。

I think this is a good approach because you can keep your feature branch up-to-date with master with your new code always on top of it and you can solve any potential conflicts directly on your feature branch. 我认为这是一个好方法,因为您可以使功能分支与master保持最新,并且始终将新代码放在最重要的位置,并且可以直接在功能分支上解决任何潜在的冲突。

Also the merges to back master are always clean and you can create a merge commit by using git merge feature --no-ff . 同样,与主服务器的合并总是干净的,您可以使用git merge feature --no-ff创建合并提交。

Just as a note, this approach has been working for me for a long time. 值得一提的是,这种方法已经为我使用了很长时间。 I use kdiff3 as mergetool . 我使用kdiff3作为mergetool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM