DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Make VS Code Your Default Git Editor πŸ“
Carl Saunders
Carl Saunders

Posted on

Make VS Code Your Default Git Editor πŸ“

Recently I've found myself using the git command git commit --amend to change typos in my commit messages. By default the GNU nano text editor is used, which for me isn't a great experience and was starting to BUG me!

Default GNU nano editor for changing git message

GNU nano editor is used by default to change git commit message

I use VS Code daily and as I'm typing the git command in the built-in VS Code terminal, naturally I want to edit the commit message using the same editor. Well luckily you can configure this with a one liner.

Default VS Code As The Git Editor (Globally)

Type the following in the command prompt / bash shell.

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

Or, alternatively if you don't like typing too much then use.

git config --global core.editor "code -w"

Note: The --wait or -w flag is crucial without this git won't know the editing has completed and in turn won't finish executing the git command.

See It Working!

Below is a gif showing VS Code as the default editor for git.

VS Code As The Default Git Editor

Working example of VS Code as the default git editor

Default VS Code As The Git Editor (Local Repo)

But wait there's more, if you don't want the change to happen globally you can set it locally for a git repository by using the flag --local.

git config --local core.editor "code -w"

Revert Back To GNU nano (or default)

And if you prefer GNU nano and want to reset this to the default git editor, then run the following command.

git config --global --unset core.editor

Note: If you set the editor locally and you want to reset then replace the --global flag with the --local flag instead.

Top comments (12)

Collapse
 
victorcazanave profile image
Victor Cazanave

Useful and clear article! πŸ‘

Although we can change the last commit message directly in one command (git commit --amend -m "New message"), configuring the git editor is useful for other commands like interactive rebase (git rebase -i).

Moreover I think the --local flag is the default value, so it's not necessary: git config core.editor "code -w".

Collapse
 
flipjorge profile image
Filipe Jorge

Nice addition! I always use -m "Message". But for interacative rebase is a pain to use vim, always need to check a cheatsheet to use it!

Thanks for this article, will set Code as default editor

Collapse
 
batisteo profile image
Baptiste Darthenay

I just found out the flag --disable-extensions to, well, disable all extensions for this session. This would speed up the launch time of code.

Collapse
 
tlstechtrekker profile image
Tami Schultz

Thank you for the info Carl - but I'm still stuck. If you or anyone else can help resolve this for me, it would be much appreciated ;-) I'm in the middle of a Git course on Udemy and no answers are coming from the Q & A there, nor am I finding anything with my stack overflow and other forum searches.

I'm getting very similar error messages when trying to either 1) alter my commit message or 2) use rebase to squash commits.

I can alter the commit message only the normal way using the -m "commit message goes here" syntax, but just using the --amend syntax gives me the following error:

Image description

When I tried implementing rebase to squash commits, I got basically the same error only without the additional suggestion on how to solve the problem:

Image description

Is it possible that the .cmd extension is the problem -- is the right file to show the GNU editor being accessed??

Image description

I checked out the bin directory just to see what files were in it - this is what it shows: 2 files - code and code.cmd:

Image description

Here are screenshots of the plain code (no extension) file; I didn't want to try to open the code.cmd file in case it activates something and messes VSC and/or Git up on my machine.

Image description

Image description

When I installed Git, I set it up to use VS Code as the default editor - here are Git installation shots:

Image description

Image description

Image description

I am not comfortable yet with tinkering with software config stuff - I work "blind" in that area of tech knowledge -- so if any kind soul has "Dummies Guide" solution or can point me to one, I'd be very grateful!

Collapse
 
andygr1n1 profile image
Andrew

thanks, very usefull

Collapse
 
baso53 profile image
Sebastijan Grabar

If I was using VS Code as commit message editor, I would even feel like I'm writing a commit message.

Collapse
 
khrome83 profile image
Zane Milakovic

Very cool. I never got the code command to work on my machine. I probably need to manually have it added to my profile or something.

I love this idea though. Nice write up.

Collapse
 
stephanie profile image
Stephanie Handsteiner • Edited on

Yup, you have to add it to your path.

You can do that from within VSCode, just search for 'shell' in the command palette and you'll see β€œShell Command: Install code in PATHβ€œ, simply do that and afterwards you may use code in your external shell.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Isn't Vim the default? I changed it to nano, BTW.

Collapse
 
mateiadrielrafael profile image
Matei Adriel

For me it was nano, idk, now I'm so used to editing it with nano I don't think I'll ever chabge it lol

Collapse
 
hernandonj profile image
Hernando NJ

Great article! Quick and effective!

Collapse
 
schartun profile image
schartun

Thanks for the useful and clear article, I read about it at The Odin Project, but you gave me more details.

DEV

Thank you.

Β 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.