在 Git 中将一个功能分离到一个新分支中
场景如下:我为代码库创建了一个新的存储库并进行了一些提交,但我决定该代码库的某些部分应该位于其自己的分支中,而不是位于 master 上。如何将该代码分离到另一个分支中,以便 master “不知道”它?显然,我不能只创建分支,然后从 master 中删除代码,因为任何后续从 master 到新分支的合并也会删除分支中的代码。
我确信这有一个微不足道的答案,但我不知道应该如何完成:)
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. 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.
I'm sure this has a trivial answer, but I can't think how it should be done :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以对 master 进行变基(即没有其他人拥有您想要移动到分支的更改),我将使用 git rebase -i 重新排列 master 中的提交,以便所有应该在毕竟分支是应该保留在 master 中的提交。之后,创建分支并重置 master,以便它不包含任何分支提交。
例如,如果您有提交(其中
B
表示您想要移动的提交,M
表示应该保留的提交):将它们重新设置为
创建新分支并
git reset --hard
master 到M3
。如果您无法对 master 进行变基,请创建您的分支,
git 还原
您不想在 master 上进行的更改,然后执行git merge -s ours master
。这有效地告诉分支您知道恢复并且您不希望它在那里。之后,如果将 master 合并到分支中,master 中的更改会正确合并,但恢复不会。更重要的是,如果您随后决定分支已完成并且想要合并回主分支,它也可以正常工作 - 分支中的所有更改都会合并,包括那些已恢复的更改。
If you can rebase master (i.e. 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. After that, create the branch and reset master, so that it doesn't contain any of the branch commits.For example, if you had commits (where
B
denotes a commit you want to move, andM
one that should stay):rebase them into
Create the new branch and
git reset --hard
master toM3
.If you can't rebase master, create your branch,
git revert
the changes you don't want on master, and then dogit 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. 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:
Now cherry-pick each commit
Now remove the same ones in master
Now remove the commits that you cherry-picked.
Hope this helps
检查 master,并编辑工作目录以删除(或隐藏?)该功能,然后提交。
为“新”功能开发创建一个分支(我将其称为“dev”,但您应该将其命名为有意义的名称)。然后在开发上,恢复(而不是重置!)之前的提交,撤消您刚刚所做的“删除”(或隐藏)工作。将该版本提交到 dev 分支。
第三步,盈利。现在您将了解如何创建一个没有该功能的主分支以及一个具有该功能的主题分支。
Checkout master, and edit the working directory to remove (or hide?) the feature, and commit that.
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, undoing the "removing" (or hiding) work you just did. Commit that reversion into the dev branch.
Step 3, profit. You now how a master branch without the feature, and a topic branch with it.