如何拆分主分支?

How to split a master branch?

我发现了如下类似的问题:

This question already has an answer here:

Change first commit of project with Git? [duplicate]

Edit the root commit in Git?

另外,我发现了很棒的 youtube 视频 how to split commit

但是,其他软问题或youtube 并没有完全解释为了拆分根提交需要做什么。

让我描述一下我所拥有的: 假设我有两个提交的 "master" 分支:

  1. root : "init" ce0418e
  2. "Commit01" 8585a44

我还有另一个分支 "anotherBranch",有三个提交:

  1. root : "init" ce0418e
  2. "testCommit01" 459ca66
  3. "testCommit02" f9f0ba4

所以我想拆分提交 "init"(它包含文件:.gitignore;README.md;myClass)

我需要文件 .gitignore 和 README.md 成为新 "init" (新根目录)的一部分,而 myClass 成为另一个带有消息 "myClass Splitted" 的提交并成为主分支的一部分.

从其他软问题中我发现我应该进行下一步:

  1. $git rebase -i --root

  2. 出现新的window并在ce0418e前面从"pick"变为"edit" "init"然后esc然后shift +':'+ 'wq'

  3. 现在我收到消息:

Stopped at ce0418e.. init

You can amend the commit now, with

git commit --amend 

Once you are satisfied with your changes, run

git rebase --continue
  1. 所以我制作 $git commit --amend 我收到 windows 声明:

init

 Please enter the commit message for your changes. Lines starting
 with '#' will be ignored, and an empty message aborts the commit.

 Date:      Tue Dec 26 18:01:07 2016 +0100

interactive rebase in progress; onto 11448c4
 Last command done (1 command done):
    edit ce0418e init
 Next commands to do (2 remaining commands):
    pick 8585a44 Commit01
 You are currently editing a commit while rebasing branch 'master' on '11448c4'.


 Initial commit

 Changes to be committed:
       new file:   .gitignore
       new file:   README.md
       new file:   myClass

再一次,我需要单独的文件:.gitignore 和 README.md 来自文件:myClass 但是怎么办?

显然,我需要在这个新的 window 中做一些更改,但是有哪些更改? 我试图通过 $git status 进行拆分,但它似乎不适用于 root 提交。

所以我的问题是接下来我该做什么?

因此您当前的 git 日志结构如下:

A---B     master
 \
   C---D  anotherBranch

A for "init" ce0418e

B for "Commit01" 8585a44

C for "testCommit01" 459ca66

D for "testCommit02" f9f0ba4

现在您想将其更改为:

A’---B’---E   master
 \
   C’---D’    anotherBranch

A’, B’, C’ and D’ only contain .gitignore and READMA.md (delete myClass) and add a new commit for master branch contain myClass file.

所以你可以这样做:

  1. 通过 git checkout mastergit commit -am 'myClass Splitted'
  2. 为主分支添加新提交
  3. 重写主分支(图中的 A 和 B)git rebase -i --root

在交互式 windows 中,将 A 和 B 更改为编辑,edit ce0418eedit 8585a44 以及 pick E

stopped at ce0418e.. init,删除工作目录下的myClass,输入git add myClassgit rebase --continue

对于8585a44 (master|REBASE-i 2/3),也删除myClass,然后输入git add myClassgit rebase --continue

  1. 通过 git checkout anotherBranchgit rebase -i --root 重写 anotherBranch。

在交互式window中,将C和D更改为编辑,edit 459ca66edit f9f0ba4

然后删除myClass,和master分支一样做