Everyone and their mother seem to be using Git these days. And I have to admit that it is really a clever piece of code. However, sometimes it may not work like you want it to do. Let me share a personal story.
Assume we have this piece of code.
if (1 > 0) { if (2 > 1) { console.log('2 is greater than 1.'); } else { console.log('Something is wrong!'); } } else { console.log('Something is really wrong!'); }
Not that it makes a lot of sense, but let us commit it to our repo anyway:
git init git add git-trip-up.js git commit -m "Initial commit"
Now assume that we want to add something to both then- and else-branches of the outer if
:
if (1 > 0) { console.log('1>0'); if (2 > 1) { console.log('2 is greater than 1.'); } else { console.log('1<=0'); console.log('Something is wrong!'); } } else { console.log('Something is really wrong!'); }
Being a nice programmer, we do it on a separate branch. Having a moment of stupidity, we do it wrong (the second console.log
is in the wrong else-branch).
git checkout -b new_branch git commit -am "Bad commit"
We make a PR, and it accidentally gets merged. Then the team leader notices that something went wrong, reverts the merged commit and tells the author that he screwed up.
git checkout master git merge new_branch git revert --no-edit HEAD
The author fixes that on his branch.
git checkout new_branch
if (1 > 0) { console.log('1>0'); if (2 > 1) { console.log('2 is greater than 1.'); } else { console.log('Something is wrong!'); } } else { console.log('1<=0'); console.log('Something is really wrong!'); }
Then he wants to make sure that there are no conflicts, and merges master
into feature-branch
. Boom! The console.log('1>0');
magically disappeared!
git commit -am "Fix a stupid mistake" git merge --no-edit master
Well, of course this is not unexpected in technical terms. But the workflow seems reasonable, merging does not actually signal any conflicts, so one might think everything is fine.
I don’t know how to prevent such mistakes, other than being alert. So be alert when merging things that involve reverting. Probably.