Don't Trust Git Conflicts

2017-05-12

tl;dr

Rebase and run tests before every merge.

How Bugs Get Introduced Even Without Git Conflicts

Here’s the situation:

  1. You test master, tests pass.
  2. You test Branch A, tests pass, no conflicts, you merge.
  3. You test Branch B, tests pass, no conflicts, you merge.
  4. Now, you test master, and tests fail!

What the what!? It’s a bit counter-intuitive, but makes sense.

Check out this great example, supplied by my dear friend and colleague Keenahn T. Jung.

git-conflit-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Branch: master ------------------------------------
// a.js
const b = require('b')
const foo = () => {
if (b % 3 === 0) return 'hi'
return 'bye'
}
module.exports = { foo }
// b.js
module.exports = 6
// a.spec
const a = require('a')
describe('a.foo', () => {
it('should return hi', co.wrap(function* () {
assert.strictEqual(a.foo(), 'hi')
}))
})
// Branch: A -----------------------------------------
// a.js
const b = require('b')
const foo = () => {
if (b === 6) return 'hi'
return 'bye'
}
// Branch: B -----------------------------------------
// b.js
module.exports = 3

The Solution

Rebase and run tests immediately before every merge.

On Github…

you can prevent this situation. Set it up in Settings -> Branches -> Branch Protection.

Github prevent merge outdated branch


Comments: