Why you should try to make commits as small as possible or else face the disaster.
Let’s assume that you are doing an awesome task that requires lots of changes in multiple files across the project. You create a branch, do everything and when you are done create one giant commit from the things that you did. You push it to the remote source control system and open the pull request. You wait with a feeling that people will admire your work, as you have changed and added a lot of cool stuff to the code, but instead people will either:
- not check the whole PR, as it takes way too long to understand what is going on
- find a lot of minor issues (misspells, unnecessary imports, …)
- skip some important issues, as they focus more on things that are easy to spot
- be scared to give you an approval as after you merge your PR, lots off changes in your PR can cause merge conflicts and hidden test failures and possibly some hidden bugs
As you can see it seems to be a bad situation, but wait, it can get worse. Let’s assume that your PR was reviewed and there are some things that needs to be changed (code smells, formatting, some logic, …). You sit and start fixing them, and in the meantime someone merges their PR to the main branch and … boom! You now have merge conflicts. You fix them and the issues from PR. Someone reviews and finds another things (stuff that comes from rebase or some things that were not found before). You fix it, … . The cycle can go on for a long time (I once saw a PR that was waiting to be merged for over 2 months! (it was finally declined)).
You get frustrated, but manage to finally fix all issues and merge it to the main branch and deploy the code to the production. You are happy that you finally did it, but as always there is some funny issue that was not caught by the tests and is now doing something bad to the production. You need to fix it as fast as possible, but because you have modified and added a lot of things it is hard to find the root cause of it.
As you can see there are a lot of things that speak agains making big commits. The list can go even deeper, but I think you see the point now. Of course, you should also avoid really small commits that just block everyone by taking away the build time and resources, but still, in my opinion it is far better to make a one line commit, than to make a giant one with 100s or 1000s lines of change.
What size of commits do you prefer? Please write them in the comments below.