Is it good practice to commit multiple files at once?

Viewed 2241

I am a new user to PyCharm, and recently started using its VCS (Version Control System) tool. Whenever I want to commit the changes that I made to a project, VCS opens, by default, a window with all the changes that I made to all files within that project, and suggests me to commit all of those changes in a single commit. Is this a good practice?

3 Answers

A commit should be "atomic", that is it should contain a group of related changes that are meant to work together. How many files are concerned is irrelevant. What you shouldn't do is

1/ commit unrelated changes together (ie a group of changes implementing a new feature and a single change fixing a bug) - because then you apply the bugfix alone (chery-pick) to another branch,

2/ commit only part of related changes (ie you changed the prototype of a function in a given file but didn't fix all the calls to this functions) resulting in a broken code base.

I’d suggest the following: each of your commits should present a new change in the overall idea of the code, regardless of the distribution of the commit amongst files. For example, if you’re changing a certain small feature in your program, and doing so resulted in changing multiple files for the same purpose, I’d keep it under the same commit. If you’re adding multiple features all in the same file, keep them as separate commits.

I would worry less about the number of files that you’re affecting with the commit and more about the overall functionality/idea of the code.

Yep, that's totally OK. When you take a look at other repositories i.e. at GitHub you see that they do that too. A good example is the CPython repository where commits are sometimes changing ten files.

Related