I have a master branch on git. Using EGit on Eclipse, I have downloaded on my local repo and I have made some changes on many files.
I have already made a huge chunk of changes on many tracked files locally, originally planned for one fat commit. These files have not been added to staged yet. The management wants me to split the changes into 2 independent commits, one after another. What are the steps for me to do that? How do I split the local changes into two versions(or two repos?) locally so that I can commit one after another?
I also have some changes in the same file that needs to split into two commits. How can I do that? I heard there is something called git stash but would that help me to work on one commit after another?
I am new to git, so please explain things in simple English and preferably in break-down steps. Thank you very much. Appreciated!
I would start by doing a
git reset --soft HEAD^
This would take you back in the commit history to the previous commit, just before your big commit, while preserving on disk the changes you made in the last commit.
Next step would be to create a new branch at this commit, to create your two new commits in.
git checkout -b new-branch-name
Since you have changed a lot of files I would then recommend a nice Git GUI to be able to easily pick and choose what to put in which of the commits. With a good enough GUI it is even easy to pick and choose separate sections of files to go in different commits.
Many IDEs have great Git integrations and plugins that can help you with staging different chunks of files instead of whole files.
Some IDEs I use that support this are:
Some Git GUIs that I use that support this are:
Use whatever tool you feel comfortable with and do the staging and committing with.
After your first commit, you should run your unit tests without the remaining changes that you want to have in the next commit. Here you can employ the stash command
git stash
This will create a stash with the files that have uncommitted changes. Now your working directory is clean and you can run your tests.
If you missed something you can pop the stash, i.e. the uncommitted changes, back into the the working folder using
git stash pop
Then make additional changes to the first commit with the GUI and redo the commit with the additional changes (i.e. amend) using
git commit --amend
Repeat the testing until done and then the last step would be to create a new commit with the remaining changes and of course also test them.
git stash pop
git add . # Assuming you are in the repo root folder
git commit -m "<good commit message>"
You now have the original commit split up into two commits on your new branch and can push it to the server for review.