Git – Should every git commit leave the project in a working state

git

I'm curious to know what the prevailing best practice is. Should git commits be enforced such that the project is in a working state (builds properly, all tests pass etc), or is committing broken code OK?

For example, if you waive this requirement you can be more flexible with commits (use them as logical chunks, even though the app is not in a working state etc). However if you enforce it you gain the flexibility of being able to cherry-pick any given commit later on…

Best Answer

This workflow gives good results for most large software projects that follow some version of the the Atlassian Git Flow model

  • Each merge to the branch from which the release is cut must leave the project in a working state. In Git Flow, this is called the master branch, and usually only the release engineers can merge code there. Run all your tests for each merge;

  • Each merge to the mainline development branch (in Git Flow it's called develop branch, but many use the master branch for that purpose) should leave the project in a working state (and it must build at least). Run most of your tests, except the longest running ones, accept some flakiness, don't run unaffected tests. But sometimes you'll merge two branches that both pass tests, but their merge is broken. This is okay. You'll fix this soon, and you'll make sure your release branches are good.

  • Each other individual commit has a primary goal of explaining why the change is made, and what is it for, and what parts of the project it affected. All other goals, such as leaving the project in a working state, are optional.

Related Topic