Using a text editor in Git

Now that I’ve switched from Atom to VS Code I thought it was about time I spruced up my Git config, using VS Code to power the text editing features.

For starters, you’ll want to globally set the code editor on the command line, like this…

git config --global core.editor "code --new-window --wait"

Or in the global config file (.gitconfig) this looks like…

[core]
	editor = code --new-window --wait

Now that you’ve set the code editor, if you want to edit the global config file you can with either of these commands…

git config --global --edit
git config --global -e

This will now fire up VS Code in a new window and wait for you to save the changes and exit, then focus will return to the command line and your config will have been updated.

Whilst you’ve got it open though (or open it again if you already closed it!) we can also use VS Code for the diff, by adding the following config lines…

[diff]
	tool = vscode-diff
[difftool]
	prompt = false
[difftool "vscode-diff"]
	cmd = code --wait --diff $LOCAL $REMOTE

Now if you edit a file (to create a difference) and enter the following command…

git difftool

…you will see VS Code open up, showing you your change using it’s own diff functionality.  Nice, right?

And let’s not stop there, you can do the same thing with merging as well, by editing the global config file again and adding the following config lines…

[merge]
	tool = vscode-merge
[mergetool]
	keepBackup = false
[mergetool "vscode-merge"]
	cmd = code --wait $MERGED

Now when there is a merge conflict that needs be resolved manually, you can enter the following…

git mergetool

…and you will see VS Code open up with the details of the merge conflict and the open to accept the current change, the incoming change, or both changes.  Once you save and exit, focus will return to the command line and you can commit the changes, as you usually would.

If you leave off the commit message, again VS Code will be opened to allow you to enter your commit message.  Sexy stuff!

I’m sure there are the command line die-hards out there who don’t see the need for any of this, but for the rest of us, there’s VS Code.