5

I am new to git and trying to understand the best practices about commits and merging.
Of course you shouldn't commit buggy changes to the master branch, and from what I understand you should create new branches for features / bug fixes in your project.

After testing how merging works, I see that when merging some branch B with branch A, branch A gets all previous commits of branch B in its log, but isn't that problematic if you are merging some experimental branch into your master branch ?
Because then it means that your master branch has in its history commits from experimental branch that were buggy, is this ok anyway or is it a common practice to exclude those commits when merging to master ?

1

1 Answer 1

4

Usually, you would squash your commits into one commit before transferring them to the care of someone else.

The reason is that the intermediate stages of your work are of interest to you while that work isn't finished; you might want to redo something, reverse a decision on one point, understand what the hell you can have been thinking on another point, etc.

The repository owner is much less likely to be interested in the intermediate steps you took in order to resolve a problem or to add functionality. They, and their clients, usually either want all of your work or none of it.

As an added bonus the commit log of the master branch becomes much easier to read if it's a list of meaningful changes to the code base rather than of all changes ever made.

3
  • Thank you! I think I may do that. I was surprised that my IDE UI doesn't have any option to squash. If I am interested in keeping the history of my experimental branches is there a way to do that even after I remove them? Feb 21, 2017 at 11:25
  • 7
    Ideally commit is a unit of work, not a complete feature. Instead of squashing the whole change, group tightly coupled and related commits and squash groups instead. Large commit is harder to understand. Remember - the code is written once, but is read many times, so be sure to make it convenient for the reader to view and understand each of your commits.
    – Basilevs
    Feb 21, 2017 at 12:03
  • One more benefit of squashing when merging to master: Every commit on master works. You can checkout or revert any given commit on master and it will compile/pass tests and just work.
    – RubberDuck
    Feb 22, 2017 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.