Git — How to break a large merge request into several small ones

Ivy Lyu
2 min readMay 30, 2021
Photo by Jon Cartagena on Unsplash

The other day I was about to submit my final merge request to GitLab, but I realized that there were 33 commits and over 20 files changed, I should realize it in the very beginning, but it was hard when I started to work on a feature and don’t want to break the flow, here I am, aiming to break the big merge request to several small ones.

In this scenario, I also don’t want to eliminate any of my 33 commits records, only 10 out of 20 files changes I want to migrate to another branch called feature/part-of-my-big-feature-branch

Step 1: Find where are your first commit and last commit in your branch

I’ll name the branch that has many commits feature/my-big-feature-branch, you can easily find it when you submit your merge request because you are comparing your branch to master, or in my case, I will give every of my commit message a start word, i.e. feature-100: initialize redux store, therefore, I would know where is my first commit of the current branch.

Then I can find my last commit using git log and the first one in the terminal will be the one.

Say I already find my first commit ac1e3 and the last one 9f74e

Step 2: Using git reset --soft ac1e3 to keep all the changes from the first commit to the last commit

Make sure you are on the latest commit of your branch 9f74eand run git reset --soft ac1e3 , in this step, you will reset your HEAD to the commit ac1e3 with all the changes staged between ac1e3 to 9f74e

Step 3: Unstage files you want to keep in this branch by running git reset filenameand stash the rest of the changes to move to another branch

Step 4: Using git reset --soft 9f74e to the last commit

After running this one, you would see two versions of the files you want to include in the commit, just run git commit -m "msg you want to add" you would see a new commit, and your MR will only reflect the files you reset in step 3

Step 5: Move to branch feature/part-of-my-big-feature-branch and apply the latest stash record and commit them, you will see other files in the feature/part-of-my-big-feature-branch has committed to feature/part-of-my-big-feature-branch

PS: Make sure you understand the following concept in git before you read this article

  1. HEAD
  2. git reset - -soft
  3. stage/unstage status

Ciao!

--

--