在 Git 中将一个功能分离到一个新分支中

发布于 2024-11-15 10:22:57 字数 208 浏览 1 评论 0原文

场景如下:我为代码库创建了一个新的存储库并进行了一些提交,但我决定该代码库的某些部分应该位于其自己的分支中,而不是位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-11-22 10:22:57

如果您可以对 master 进行变基(即没有其他人拥有您想要移动到分支的更改),我将使用 git rebase -i 重新排列 master 中的提交,以便所有应该在毕竟分支是应该保留在 master 中的提交。之后,创建分支并重置 master,以便它不包含任何分支提交。

例如,如果您有提交(其中 B 表示您想要移动的提交,M 表示应该保留的提交):

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

将它们重新设置为

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

创建新分支并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, and M one that should stay):

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

rebase them into

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

Create the new branch and git reset --hard master to M3.

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. 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.

草莓酥 2024-11-22 10:22:57

您可以通过将您想要的提交挑选到它自己的分支中来做到这一点。

在您想要私有的第一个提交之前,首先对提交进行分支:

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

现在挑选每个提交

git cherry-pick hash1 hash2 etc

现在删除 master 中相同的提交

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

现在删除您挑选的提交。

希望这有帮助

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

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

Now remove the commits that you cherry-picked.

Hope this helps

一曲琵琶半遮面シ 2024-11-22 10:22:57

检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文