Home

Don't trust git conflicts

Gunar Gessner

Gunar Gessner

Feb 21, 2019

tl;dr — Rebase and run tests before every merge (solution).

# Study case

Here’s an example that generates zero git conflicts and yet introduces a bug.

  1. We test master, tests pass ✅

    app.js

    const config = require('config.js');
    
    const greeting = () => {
      if (config.index % 3 === 0) { return 'hi' };
      else { return 'bye' };
    }
    
    module.exports = { greeting }

    config.js

    module.exports = { index: 6 };

    test.js

    const app = require('app.js');
    assert(app.greeting() === 'hi');
  2. We test Branch A, tests pass ✅, no git conflicts ✅, we merge 🔽

    Branch A

    @@ -1,7 +1,7 @@
    const config = require('config.js');
    
    const greeting = () => {
    
  • if (config.index % 3 === 0) { return 'hi' };
  • if (config.index === 6) { return 'hi' }; else { return 'bye' }; }
  1. We test Branch B, tests pass ✅, no git conflicts ✅, we merge 🔽

    Branch B

    @@ -1 +1 @@
    -module.exports = { index: 6 };
    +module.exports = { index: 3 };
  2. Now, we test master, and tests fail! ❌

    Expected values to be strictly equal:
    
    'bye' !== 'hi'

# What happened?

It’s a bit counter-intuitive. Take a minute to study the case above.

# Solution

Rebase and run tests immediately before every merge. On Github you can set up a branch protection rule like this:

Enabled option "Require branches to be up to date before merging"


Sign up for the newsletter


Read other stuff