简体   繁体   English

将功能分离到 Git 中的新分支中

[英]Separating a feature into a new branch in Git

Here's the scenario: I've created a new repository for a codebase and made a few commits, but I've decided there's some part of that codebase that should be in its own branch rather than on master.场景如下:我已经为代码库创建了一个新的存储库并进行了一些提交,但我决定该代码库的某些部分应该在它自己的分支中而不是在 master 上。 How do I separate that code off into another branch so that master is "unaware" of it?如何将该代码分离到另一个分支中,以便主人“不知道”它? Obviously I can't just create the branch and then delete the code from master, since any subsequent merges from master into the new branch will delete the code in the branch too.显然,我不能只创建分支然后从 master 中删除代码,因为从 master 到新分支的任何后续合并也会删除分支中的代码。

I'm sure this has a trivial answer, but I can't think how it should be done:)我确信这有一个微不足道的答案,但我想不出应该怎么做:)

If you can rebase master (ie nobody else has the changes you want to move to the branch), I would use git rebase -i to rearrange the commits in master, so that all commits that should be in the branch are after all commits that should stay in master.如果你可以 rebase master(即没有其他人有你想要移动到分支的更改),我会使用git rebase -i重新排列 master 中的提交,以便所有应该在分支中的提交毕竟是应该留在主人。 After that, create the branch and reset master, so that it doesn't contain any of the branch commits.之后,创建分支并重置 master,使其不包含任何分支提交。

For example, if you had commits (where B denotes a commit you want to move, and M one that should stay):例如,如果您有提交(其中B表示您要移动的提交,而M表示应该保留的提交):

M1---B1---M2---B2---M3

rebase them into将它们重新设置为

M1---M2---M3---B1---B2

Create the new branch and git reset --hard master to M3 .创建新分支并git reset --hardM3

If you can't rebase master, create your branch, git revert the changes you don't want on master, and then do git merge -s ours master .如果您无法重新设置 master,请创建您的分支, git revert您不希望在 master 上进行的更改,然后执行git merge -s ours master This effectively tells the branch that you know about the revert and you don't want it there.这有效地告诉分支你知道恢复并且你不希望它在那里。

After that, if you merge master into the branch, changes in master are correctly merged, but the revert isn't.之后,如果您将 master 合并到分支中,master 中的更改会正确合并,但还原不会。 What's more, if you then decide the branch is done and you want to merge back into master, it works correctly too – all changes from the branch are merged, including those that were reverted.更重要的是,如果你决定分支已经完成并且你想合并回master,它也可以正常工作——来自分支的所有更改都被合并,包括那些被恢复的更改。

You can do it by cherry-picking the commits you want into it's own branch.你可以通过挑选你想要的提交到它自己的分支来做到这一点。

First branch off of the commit before the first commit you want private:在您希望私有的第一个提交之前从提交的第一个分支:

git checkout -b feature1 sha1^ # sha1 is the hash of the first private commit

Now cherry-pick each commit现在挑选每个提交

git cherry-pick hash1 hash2 etc

Now remove the same ones in master现在删除master中相同的

git checkout master
git rebase -i sha1^ # as in the first checkout

Now remove the commits that you cherry-picked.现在删除您挑选的提交。

Hope this helps希望这可以帮助

Checkout master, and edit the working directory to remove (or hide?) the feature, and commit that. Checkout master,并编辑工作目录以删除(或隐藏?)该功能,然后提交。

Make a branch for the "new" feature development (I'll call it "dev", but you should name it something meaningful).为“新”功能开发创建一个分支(我将其称为“开发”,但您应该将其命名为有意义的名称)。 Then on dev, revert (not reset,) the previous commit.然后在 dev 上,恢复(而不是重置)之前的提交。 undoing the "removing" (or hiding) work you just did.撤消您刚刚所做的“删除”(或隐藏)工作。 Commit that reversion into the dev branch.将该还原提交到 dev 分支。

Step 3, profit.第三步,盈利。 You now how a master branch without the feature, and a topic branch with it.您现在如何在没有该功能的情况下创建一个 master 分支,以及一个带有它的主题分支。

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

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