VS Code works better with Git

We interrupt with a commercial:

We are the front end team of Feishu, if you are passionate about technology and want to experience the rapid growth of business, welcome to join us, please pay attention to the internal push link.

For more interview questions, add wx: ruanxiaoting_

Use VS Code as your default Git editor and git DiffTool as your default editor. Let’s see how to do it!

TLDR

To get Git to use VS Code by default, you first need to make sure you can run VS Code from the command line, as described in the Prerequisites section.

Then run git config — global-e to edit the global configuration and add the following:

[core]
  editor = code --wait
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = code --wait $MERGED
Copy the code

Why make VS Code the default Git editor, Diff Tool or Merge Tool?

It’s a personal choice! There are many, many choices. Most importantly, tools should complement the workflow, not hinder it.

I’ll explain my decision and maybe it will give you some insight into what’s best for you. In short, I prefer to do as much as POSSIBLE in my code editor.

We may encounter the following awkward situations:

  1. If I’m executing an interactive Git command that requires my input to edit and view a chunk of text, I prefer to stay in my code editor and keep the same mind-set.
  2. I haven’t used some of the Git-related Linux command line tools like Nano to get the necessary muscle memory, I forgot the commands! 🙈 could hardly get in the way.
  3. I generally prefer less switching between applications. I prefer to switch to another TAB in the code editor rather than a separate window.
  4. For the difference, I prefer to see it in the editor of the GUI.
  5. Some merge conflicts are quite demanding and I like to jump to the source file to get the full image, if I can do it in VS Code I can use familiar shortcuts.
  6. If I can do all this in my code editor, I have a consistent color theme with no further configuration required.

A prerequisite for

Make sure you can run VS Code from the command line before setting it as the default editor, Diff Tool or Merge Tool. You may need to install it.

To test this, run the command code –help from the command line. If you don’t see some help output, it means you can’t currently run VS Code from the command line.

This can be corrected by following these steps:

  • Windows: You need to edit the environment variables and add the VS Code installation location toPATHVariable. Or you can reinstall and make sure it selects it in the Installation wizard (there is an option).
  • MacOS:Shell Command: Install 'Code' command in pathSelect from the command panel.
  • Linux: Be sure to use *.deborThe.rpm* package installs VS Code.

Make VS Code the default editor

The default Git editor is Nano.

This is how Nano looks for commit messages.

This is how VS Code looks for commit messages.

configuration

To update your Git configuration, run the following command:

git config --global core.editor 'code --wait'
Copy the code

If you want to open a new window every time, add the –new-window code flag:

git config --global core.editor 'code --wait --new-window'
Copy the code

If you only want to change it for your current project, run the same command without the * — global* git flag.

Unhappy and want to go back?

git config --global --unset core.editor
Copy the code

Set VS Code to the default Diff Tool

The default Diff Tool is vimdiff.

Specifying the Diff Tool affects the Git difftool command. Git Diff uses DiffTool to compare differences on the command line. The Difftool command initiates an interactive dialogue that asks you to select which changing files to open.

That’s how Vimdiff looks for differences. Through 🕶!

This is how VS Code looks for differences.

You’ll notice on the command line of the screen shot above that my Diff session shows 13 files that have changed. If the list of files is long, you can always cancel the process in the typical way: Ctrl+ C. You may need to narrow down the command to make the collection more manageable.

configuration

To configure it from the command line:

git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
Copy the code

This adds the following Settings to your global Git configuration:

[diff]
	tool = vscode
[difftool "vscode"]
	cmd = code --wait --diff $LOCAL $REMOTE
Copy the code

You can choose 😁.

If you don’t like VS Code as a Diff tool, run git difftool –tool-help to see more options.

Makes VS Code the default Merge Tool

Git does not set the default Merge Tool.

When a conflict occurs, you receive an error message when you try to pull or push changes. Running Git MergeTool will allow conflict resolution.

Run vimdiff and it looks like this:

This is what merge conflicts look like in VS Code:

CodeLens provides options for resolving conflicts. If there are more than one conflict, a toolbar appears in the upper right corner of the document with options for listing each conflict.

configuration

To do this from the command line:

git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
Copy the code

This adds the following Settings to your global Git configuration:

[merge]
	tool = vscode
[mergetool "vscode"]
	cmd = code --wait $MERGED
Copy the code

You can choose 😁.

If you don’t like VS Code as a Merge Tool, run git mergetool — Tool -help to see more options.

conclusion

Setting up VS Code to manage all git requirements is simple. If you want to use VS Code or stick with command-line tools, it’s a matter of personal preference.

Happy Coding! 🙂

The original link: www.roboleary.net/vscode/2020…