Skip to content

Git : split a commit

In case you make the same mistake as I did, I would like to share with you my Commit Splitting Adventure.

Indeed today when I tried to push my last pack of commits I had the bad surprise of being rejected by the server. Have a seat, make yourself confortable and let’s go !

The error

As you can see in the push message, GIT tells me that the server rejects my pack of commits because it is too heavy.

Tip

It is important to make the difference between commit and pack of commit

remote: fatal: pack exceeds maximum allowed size

Issue_with_push

The Solution

Warning

To solve this with have to split the commit

The things is that the commit is already done. So we have to modify the history of the commits Rebasing to solve this.
We will follow thoses steps to come back on track.

Tip

If the commit pack you want to push is the last one (be sure of it by checking the log) The you can directly start from the step below.

1. Which commit do we want to split : “git LOG

We check the log to find our commit

git log –> gives the history of commits

The one which is too big is “Update of CAD week02 and center some images”.
We can also see that on origine, “head” and “master” are one commit before.

Check the log

Now I want to be sure that the commit is the correct one. So I will look into it

git diff-tree HEAD~2 -r –> gives the content of the commit “Minus 2” before local head.

The big files are here (archives tar.gz). This commit has to be split. Check the log

2. Modifying the log : “git REBASE

So here is the trick. In the previous pictures, if counting from 0, my commit is the 2rd in the list.

  1. Adding images modifications and mkdocs.yml url update
  2. Changing some pictures to use thumbnails
  3. Update of CAD week02 and center some images

So when you rebase, you have to rebase starting before your commit (wich is “minus-2”) so “minus 3”.

git rebase -i HEAD~3

It will open your favorit text editor to modify the actions to be done. For the commit you want to modify you have to change pick for edit and save.
In my case you can see that I put Edit in front of “Update of CAD week02 and center some images” Editing the history

Quit and save the previous window. You will see the following information in your terminal.
You are modifying the master branch. Take care ! Rebase starts

3. Now you can execute the splitting command : “git RESET

git reset HEAD~ –> because now we are at the top of the log.

This will “break” your commit Reset HEAD

If you check the status of your repo you will find your files out of any commit. You can add them as you would do normally.

This is the most important info of this page

This time TAKE CARE to commit a reasonable amount of files and if you expect a big commit, push it alone.

   git satus --> to check what has to be done  
   git add file_1  
   git add file_2  
   git commit -m "your message"  
   git push   
        You pushed the first part of your big commit
   git add file_3  
   git add file_4  
   git commit -m "your message"  
   git push 
        You pushed the second part of your big commit
   git rebase --continue
        You tell git to restart normal logging

Git status and ADD

3. Final Log

If you now look at my log, you can see 2 new commits (1 and 4).

  1. Commit 1/2 with stl part to repare previous commit
  2. xxx
  3. xxx
  4. git rebase to split commit 435....

They correspond to the full commit I had done before.

Info

They are two commits in between (the xxx) because I used the function git rebase --continue BEFORE pushing my two commits

Git status and ADD

I hope I have not lost to much of you on the way.

4. Conclusion

Do not push heavy commits !

5. Functions Summary

A little summary of all the functions used here :

  • git log -> log of commits
  • git diff-tree HEAD~2 -r -> shows the difference with respect to previous commit
  • git rebase -i HEAD~3 -> place logging at HEAD-3
  • git reset HEAD~ -> supress the last commit
  • git add
  • git commit
  • git push
  • git rebase --continue –> start logging again at HEAD-
  • git show-branch –> shows you a nice view of your branches in your terminal

Last update: February 9, 2021