简体   繁体   English

如何将分支分成功能分支

[英]How to separate a branch into feature branches

I have a codebase that didn't use git so far. 到目前为止,我有一个代码库没有使用git。 I've made a new branch and did some cleaning. 我做了一个新的分支并做了一些清洁工作。 The last three commits are: 最后三个提交是:

bdd0f42ca51734a10a09985fa65abb81c876432d deleted Feature 2
ef236bb88de9aa4e4839b52b7a5131e437174d48 deleted Feature 1 <-- Feature 2 is still in it
db3be0c46e7e5abc3f234a675424cd92f0dcd78e deleted Garbage <-- Feature 1 and Feature 2 is still in it

The current head is the desired starting point for further development, ie the initial state of release, develop and master. 当前的头是进一步发展的理想起点,即发布,开发和掌握的初始状态。

How can I save Feature 1 and Feature 2 into two separate feature branches, that can be merged later as usual? 如何将功能1和功能2保存到两个单独的功能分支中,可以像往常一样合并?

you might consider this: 你可能会考虑这个:

git checkout -b feature/2 # create feature/2 branch
git revert bdd0f42

the git revert will create the inverse commit of commit bdd0f42 hence adding feature 2 again git revert将创建commit bdd0f42的反向提交,从而再次添加功能2

then for feature 1: 然后是功能1:

git checkout -b feature/1 bdd0f42 # create feature/1 branch from your HEAD commit
git revert ef236bb

this might be a little more tricky; 这可能有点棘手; since code changes between feature1 and feature2 might affect the same files... 因为feature1和feature2之间的代码更改可能会影响相同的文件...

I assume by saying "deleted Feature X" you mean you reverted the code with that feature. 我假设你说“删除功能X”,你的意思是你使用该功能恢复了代码。 Further, as you have reverted these features you should also be having commits having the code for the features. 此外,当您还原这些功能时,您还应该拥有具有功能代码的提交。 So your commits would look like (reverse chronological) : 所以你的提交看起来像(反向时间顺序):

(a4s5s2) * deleted Feature 2
         |
(d2r3t4) * deleted Feature 1
         |
(fd32d3) * deleted Garbage
         |
(dt3d23) * added Feature 2
         |
(dw24d1) * added Feature 1
         |
(23d234) * other commits..

Where the alphanumerical values written in parenthesis are the last parts of the commit ids. 括号中写的字母数字值是提交ID的最后部分。

Now coming on to the solution for your problem : 现在为您的问题找到解决方案

  • Go to commit (dw24d1) * added Feature 1 by using command git checkout dw24d1 then create a new branch from this commit using command git checkout -b feature1 转到commit (dw24d1) * added Feature 1使用命令git checkout dw24d1 (dw24d1) * added Feature 1然后使用命令git checkout -b feature1从此提交创建一个新分支

  • Similarly, go to your main branch using command git checkout main-branch , go to commit (dt3d23) * added Feature 1 by using command git checkout dt3d23 then create a new branch from this commit using command git checkout -b feature2 类似地,使用命令git checkout main-branch转到git checkout main-branch ,转到commit (dt3d23) * added Feature 1使用命令git checkout dt3d23 (dt3d23) * added Feature 1然后使用命令git checkout -b feature2从此提交创建一个新分支

  • Finally to come back to your latest commit on your main branch using command git checkout main-branch 最后使用命令git checkout main-branch回到主分支上的最新提交

After doing all this what you will achieve is 3 branches - main-branch (containing latest code), feature1 (containing feature 1), feature2 (containing feature 2). 完成所有这些后,您将实现的是3个分支 - 主分支(包含最新代码),feature1(包含特征1),feature2(包含特征2)。


Edit: 编辑:

As you mentioned, you do not have the commits for the features added. 如您所述,您没有添加功能的提交。 In this case, you can simply do the following : (considering you are at latest commit of your main branch, say master ) 在这种情况下,您可以简单地执行以下操作:(考虑到您最近提交的主分支,请说master

  • Create new branch for feature 1 using command git checkout -b feature1 , then commit feature 1 code on this branch 使用命令git checkout -b feature1为功能1创建新分支,然后在此分支上提交功能1代码
  • Go back to master branch using command git checkout master , then create new branch for feature 2 using command git checkout -b feature2 , then commit feature 2 code on this branch 使用命令git checkout master返回master分支,然后使用命令git checkout -b feature2为功能2创建新分支,然后在此分支上提交功能2代码
  • Go back to master branch using command git checkout master and continue work. 使用命令git checkout master返回master分支并继续工作。

Suppose you have a new_release branch with having last 3 commits 假设您有一个new_release分支,其中包含最后3次提交

  1. feature 1 特色1
  2. feature 2 特色2
  3. feature 3. 特色3。

Now, you have created a new branch ( feature_1 ) from dev . 现在,您已从dev创建了一个新分支( feature_1 )。

you can simply cherry-pick feature1 commit by commit Id . 你可以简单地通过commit Id cherry-pick feature1提交。

In feature_1 branch you can do, feature_1分支中,您可以这样做,

 git cherry-pick {commitIdOfFeature1}

After that, your feature_1 will have all commits of dev and the commit you have given id. 之后,您的feature_1将拥有dev的所有提交以及您已提供id的提交。

In this way, you can create different branches for your different requirement. 通过这种方式,您可以根据不同的需求创建不同的分支。

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

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