Partilhar via


Keeping master history clean

On a previous team we moved from using our own internal source control provider to Git. Having never used Git myself at the time I was excited to try something new. I instantly fell in love.

 

Unfortunately not everyone on the team fell in love and many were still operating as if they were using traditional source control. Mistakes were getting made daily, poor commits were being pushed the server as well as bad merges. It got to the point that the team was locking master down at least once a day to clean it up. Eventually I was fed up and decided this problem is easily solvable with a script.

 

As a team we had agreed on the following:

  • History should be linear

What do you mean by linear history?
The history must only have one path can be followed. I've shared a few commits below that should make this easier to visualize.

Figure A, represents a clean linear history with no branches.
Figure B, represents a clean linear history with a simple branch. This is still considered linear because there are no overlapping commits. This is an example of what the history might look like when performing --no-ff merges.
Figure C, represents an invalid commit. The second commit from the top is out of place. No two commits should be able to have the same parent and child
Figure D, also represents an invalid commit . The second commit from the top is out of place.
    
The first version of the validation script was simple:

git fetch origin +master:master

git checkout master

git merge --ff-only %SourceRef%

 

The above script worked great to ensure master maintained a clean history. Commits similar in shape to figures A and B would merge cleanly into master, but there were still problems. 

  • Users were still pushing commits with default user name and email
  • Commits were pushed with poor messages
  • Advanced users wanted to use --no-ff merges the above script was destroying the history

After investigating the issues a more complex PowerShell script was developed that could be run as part of the check-in process (another post coming shortly). This evolved script did a great job catching the common mistakes we were making as a team and has since kept master clean.

The evolved script can be found here: https://github.com/ericmaino/Scripts/blob/master/MergeClean.ps1