Blog

Solving git conflicts with sed

I recently experienced a git merge that kept both versions of the files and merged them together, ending in committing the merged version to the repo. Of course the software (KiCAD) wouldn’t open the files anymore.

I ended up not being able to fix the conflicts because the tools wouldn’t recognise it, and I wasn’t prepared to revert the files, as I would have lost the modifications.

The content of the KiCAD schematic files looked like this :

<<<<<<< master
F 2 “” H 9750 1950 60 0000 C CNN
=======
F 2 “IDC-34V” H 9750 1950 60 0001 C CNN
>>>>>>> local

The only way I could solve this issue is by “cleaning up” the files by keeping only the “local” bit. Fortunately, sed* is smart enough to be able to delete multiline content by using a start regex and an end regex pattern. For reference:
https://stackoverflow.com/questions/27775601/deleting-a-multiline-block-of-text-between-regex-pattern-using-sed#_=_

I used the following commands to fix my files:

$ sed -i ‘/<<<<<<< master/,/=======/d’ file.sch
$ sed -i ‘/>>>>>>> local/d’ file.sch

The first line will edit (-i: in-place) file.sch and remove everything between the “<<<<<<< master” and the “=======” lines. Leaving only the interesting bit, and the “>>>>>>> local” line. The second expression is simply taking care of the “>>>>>>> local” line!

* If you’re under linux then you already have sed. If you’re under Windows and you are using TortoiseGit with Git, then you have a very handy Git Bash installed; otherwise have a look at http://gnuwin32.sourceforge.net/packages/sed.htm.