scenario

You’re working on A project, and at some point in the past, you commit two major changes to release A. It’s only after A few more commits that you find it necessary to split those two major changes into versions A and B.

The current commit log looks like this:

commit 4a6a4088ecbe26d7f85db703e9c0a493aaac9675
Author: Wray Zheng 
Date:   Thu March 25 17:06:19 2017 +0800

    add new functions

commit 1c6a58f2c80b276b24495558cffedd13998a766a
Author: Wray Zheng 
Date:   Thu March 25 17:04:23 2017 +0800

    Two Big Changes

commit 3157d5c2c16938d2ba1d68eae8fb5a26f87365ea
Author: Wray Zheng 
Date:   Thu March 25 17:03:06 2017 +0800

    first commitCopy the code

Next, we will split the “Two Big Changes” commit into Two commits.

1. Enter the Rebase interaction mode

git rebase -i Copy the code

Here sha-1 is the last committed checksum of version A.

For example, enter the following command to enter interactive mode:

git rebase -i 3157d5cCopy the code

If version A is not the case above and is committed first, use the –root option:

git rebase -i --rootCopy the code

Git will then open a file that gives you all the commits since the specified commit for you to make changes to.

pick 1c6a58f Two Big Changes pick 4a6a408 add new functions # Rebase 3157d5c.. 4a6a408 onto 3157d5c # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented outCopy the code

2. Change the command corresponding to version A to edit

We found the commit for version A, “Two Big Changes”. Change the pick before version A to Edit. This will keep Git at version A during the rebase process, let’s make the changes, and then continue the rebase process. After modification, it looks like the following:

edit 1c6a58f Two Big Changes
pick 4a6a408 add new functions
......Copy the code

Save and close the file, and Git begins the rebase process.

3. Uncommit version A

We can see that Git is stuck at version A during the rebase process, so let’s do something about it. At this point, we’re in an environment where we’ve just committed version A. Next, undo the commit of version A, i.e. revert to the previous version of version A:

git reset HEAD~Copy the code

Then you can go to step 4.

If version A is submitted for the first time, do as follows.

To remove the second change from the staging area, use the following command:

git rm --cached change2.cppCopy the code

Now there is only the first change left in the staging area, which we can commit as version A:

git commit --amend -m "Version A"Copy the code

Then commit the second change:

git add change2.cpp
git commit -m "Version B"Copy the code

At this point, the separate commits of the changes are complete, and you can move on to step 5.

4. Commit two major changes separately

After uncommitting version A, we can commit the two major changes separately.

git add change1.cpp
git commit -m "Version A"

git add change2.cpp
git commit -m "Version B"Copy the code

5. Complete the Rebase process

Finally, just type the following command to complete the rebase process:

git rebase --continueCopy the code

And you’re done! Let’s see if the commit log is as expected at this point.

commit 27aedab1a2a3ae4abb1f194971ae773e9a8017c5
Author: Wray Zheng 
Date:   Thu March 25 17:06:19 2017 +0800

    add new functions

commit a5f065f4736c676cca27c0c716ce732f401c913e
Author: Wray Zheng 
Date:   Thu March 25 17:51:53 2017 +0800

    Version B

commit 56f506b500c1119c3736501e30c0a901a378ae03
Author: Wray Zheng 
Date:   Thu March 25 17:51:42 2017 +0800

    Version A

commit 3157d5c2c16938d2ba1d68eae8fb5a26f87365ea
Author: Wray Zheng 
Date:   Thu March 25 17:03:06 2017 +0800

    first commitCopy the code

As expected, we split version A into two commits: Version A and version B.

conclusion

Here, we use the rebase operation in interactive mode. When we make changes to version A, Git restores the state for us, including workspace, staging area, and repository, to the state just after version A was committed. In this case, HEAD is version A.

We use git reset HEAD~ to restore the repository to the previous version, while the workspace still has the changes from version A.

We then commit the two changes separately. Finally, git rebase –continue to complete the rebase operation.

copyright

The original author: Wray, Zheng links: www.codebelief.com/article/201…