-1

I found similar questions like one below :

This question already has an answer here:

Change first commit of project with Git? [duplicate]

Edit the root commit in Git?

Also, I found wonderful youtube video how to split commit.

However, other sof questions or youtube didn't explain completely what need to do in order to split root commit.

Let me describe what I've : let say I have "master" branch with two commits:

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

And I've another branch "anotherBranch" with three commits:

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

So I would like to split commit "init" ( it contains files: .gitignore ; README.md ; myClass )

I need that files .gitignore and README.md became the part of new "init" ( new root ) and myClass became another commit with message "myClass Splitted" and be the part of master branch.

From other sof questions I figure out that I should make the next steps :

  1. $git rebase -i --root

  2. in appeared new window and change from "pick" to "edit" in front of ce0418e "init" then esc then shift + ':' + 'wq'

  3. Now I receive the message :

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. So I making $git commit --amend and I receive the windows that states:

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

Once again shortly, I need separate files: .gitignore and README.md from file: myClass But how ?

Obviously, I need to make some changes in this new window but what changes? I tried to make splitting through $git status but it seems that it doesn't work with root commit.

So my question is what should I do next ?

Tim Florian
  • 408
  • 1
  • 3
  • 13

1 Answers1

0

So your current git log structure like:

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

A for "init" ce0418e

B for "Commit01" 8585a44

C for "testCommit01" 459ca66

D for "testCommit02" f9f0ba4

And now you want to changes it to:

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.

So you can do as below:

  1. Add a new commit for master branch by git checkout master and git commit -am 'myClass Splitted'
  2. Rewrite master branch (A and B in graph) by git rebase -i --root.

In interactive windows, change A and B as edit, edit ce0418e and edit 8585a44 and pick E.

When stopped at ce0418e.. init, delete myClass in working directory, and input git add myClass and git rebase --continue

For 8585a44 (master|REBASE-i 2/3), also delete myClass, then input git add myClass and git rebase --continue

  1. Rewrite anotherBranch by git checkout anotherBranch and git rebase -i --root.

In interactive window, change C and D to edit, edit 459ca66 and edit f9f0ba4.

Then delete myClass, and do the same thing as the master branch.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • thx for your response. "When stopped at ce0418e.. init, delete myClass in working directory" 1) Pls tell me what I do to delete myClass in working directory ? 2) According to your scheme do I correctly understand that I cannot split root in order that master branch looks like this : A’--E---B' master ? Many thx for response. – Tim Florian Dec 28 '16 at 13:21
  • For your questions: 1.Just in your local repo, find myClass and delete it. 2. Yes, you can make it in different order:A’-E-B’. In the interactive window you just need youe adjust the oder: `edit ce0418e`, `pick E` and `edit 8585a44`. `git rebase -i` is a powerful command, you can edit, re-order or delete a commit by it. – Marina Liu Dec 29 '16 at 01:38