Using VS Code for merges in Mercurial

VS Code is now a great visual merge tool, here is how you set it up to be the merge tool and visual diff tool for Mercurial

I’ve always struggled to find a graphical merge tool that I can actually understand and up until now I have just been using merge markers along with a handy Mercurial command to open all conflicted files in VS Code, my editor of preference.

Well it turns out that since version 1.69 VS Code now has built in support for acting as a merge tool and after trying it out I actually found it to be useful! Given that they (and the rest of the world) tend to focus on Git I couldn’t find explicit instructions for setting it up for Mercurial so here is how you do it. Add the following to your ~/.hgrc:

[extensions]
extdiff =

[ui]
merge = code

[merge-tools]
code.priority = 100
code.premerge = True
code.args = --wait --merge $other $local $base $output

[extdiff]
cmd.vsd = code
opts.vsd = --wait --diffCode language: PHP (php)

This does two things. It registers VS Code as the merge tool for conflicts and also adds a hg vsd command to open side by side diffs of individual files in VS Code.

And if you do still need to open any unresolved files in VS Code you can use this config:

[alias]
unresolved = !$HG files -T "{reporoot}/{path}\0" "set:unresolved()" | xargs -0 code --waitCode language: PHP (php)

After that running hg unresolved will open any unresolved files in VS Code.