简体   繁体   English

将git提交重组到不同的分支

[英]Reorganising git commits into different branches

I am trying to reorganise my git tree so that it is structured a bit better. 我正在尝试重新组织我的git树,使其结构更好一些。 Basically at the moment I have a single master branch with a couple of small feature branches that split from it. 基本上,目前我有一个主分支,并从中拆分出几个小功能分支。 I want to go back and reorder it so that the only commits in the main branch are the ones corresponding to new version numbers and then have all the in between commits reside in a separate develop branch from which the feature branches split from too. 我想返回并重新排序,以便主分支中唯一的提交是对应于新版本号的提交,然后将所有提交之间的所有操作都放在一个单独的develop分支中,功能分支也从该分支中​​分离出来。 Basically I'm looking for a tool that will let me completely manually reorganise the tree. 基本上,我正在寻找一种工具,可以完全手动重组树。 I thought maybe that interactive rebasing was what I was looking for but trying to do so in sourcetree makes it seem like it is not the right tool. 我以为交互式重定基准可能正是我想要的,但是尝试在sourcetree中这样做似乎使它看起来不是正确的工具。

Can anyone give me some advice on how best to proceed. 谁能给我一些有关如何最好地进行操作的建议。 Below is a diagram of my current structure: 下面是我当前结构的示意图:

featureA                       x-x-x   
                              /     \
master     A-x-x-x-x-B-x-x-x-C       D

Desired structure: 所需结构:

feature                   x-x-x
                         /    |
develop     x-x-x-x-x-x-x    -
           /      |     |     |
master    A    -  B  -  C  -  D

Having a linear history -like your current structure- is something wanted by many people. 具有线性历史记录(例如您当前的结构)是许多人想要的东西。 If you really want to try this new workflow, what about adopting it from now on, without changing the past? 如果您真的想尝试这个新的工作流程,那么从现在开始采用它而不改变过去呢? It would save you some trouble. 这将为您省去一些麻烦。

However, if you really want to change it, you could do: 但是,如果您确实要更改它,则可以执行以下操作:

#Start to put tags on 'main' commits, to ease manipulations
git checkout B
git tag tagB
git checkout B
git tag tagB
git checkout D
git tag tagD

#Start to rewrite from the oldest
#Not much to do: we just need to add a merge commit
git checkout A
git merge --no-ff B
git branch -f master

#Continue with C
#Same thing, but we need to cherry-pick the intermediate commits
git checkout tagB
git cherry-pick C~3
git cherry-pick C~2
git cherry-pick C~1
git cherry-pick C
git merge --no-ff master
git branch -f master

#Continue with D
...

To ease those manipulations and to ensure you know what is going on, you should open a gitk --all HEAD in the background, and regularly press F5 to refresh it. 为了简化这些操作并确保您知道发生了什么,您应该在后台打开一个gitk --all HEAD ,并定期按F5刷新它。

And if you think you messed up, and you want to retrieve the initial state, you'd just need to do 如果您认为自己搞砸了,并且想要检索初始状态,则只需执行以下操作

git checkout tagD
git branch -f master

A final word: if you already shared those commits (eg: if you already pushed), you should warn everyone likely to have them, that you're going to change the structure of the history. 最后一句话:如果您已经共享了这些提交(例如,如果您已经推送了),则应该警告可能拥有它们的所有人,这将改变历史的结构。 Otherwise it could lead to a real mess. 否则可能导致真正的混乱。

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

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