Home > Software engineering >  Splitting changes into 2 commits for git
Splitting changes into 2 commits for git

Time:06-24

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 files locally, originally planned for one fat commit. 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!

CodePudding user response:

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:

  • VS Code with the GitLens plugin
  • IntelliJ

Some Git GUIs that I use that support this are:

  • SourceTree
  • GitKraken

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.

CodePudding user response:

You are lacking all git basic concepts, my first suggestion is to get some basic tutorials and try. Here one from git docs: https://git-scm.com/docs/gittutorial.

Always good to use an cheat sheet, too. See this one https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet.

Now, for your questions:

The management wants me to split the changes into 2 independent commits

Commits are always independent, so:

  1. make some work
  2. add that work to staging area
    git add <file_name>
  3. and commit
    git commit -m "<commit comment>"
  4. add more work to staging area
    git add <file_name>
  5. and commit
    git commit -m "<commit comment>"

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?

This one is more trickier. It will depend on what types of changes you have and if your file is tracked or not, but I would go for:

  1. get diff of changed file
    git diff <file_name> > <file_name>_changeset_1.diff
  2. make a copy of diff file
    cp <file_name>_changeset_1.diff <file_name>_changeset_2.diff
  3. edit both diff files, and leave each one with only desired changes
  4. apply change set 1
    git apply <file_name>_changeset_1.diff
  5. commit
    git commit -m "<commit comment>"
  6. apply change set 2
    git apply <file_name>_changeset_2.diff
  7. commit
    git commit -m "<commit comment>"

Obs. I don't revert any previous commit here, if you are talking about reverting commits, pushed or not, than you need to use additional steps.


Edit

As @kadewu mentioned, there is a more easy way to staging the diffs selectively with git add -p <file_name>and using s to split the hunk in smaller hunks.

  • Related