The foreword 0.

Git (Git-scm.com) is a free and open source distributed version control system. It was originally developed by Linus, the father of Linux, using C language in 2005 for the purpose of managing the source code of Linux kernel. After that, Git is becoming more and more mature and has been widely used.

VS Code has built-in support for Git, which makes it easy to use a graphical interface for version control, such as staging, submitting updates, pushing, and pullingYou must have Git installed on your computer and know how it works.

1. Set up git. The path

Git installation can be performed by baidu itself, and the installation process is as follows:



Git bash is a command line tool used by most people in Git. In order not to affect other Windows commands,The first one is selected during installationVS Code will not be able to find Git’s location after startupgit.pathOtherwise openGit repository(If it is a common folder, use it firstgit initInitialize as Git repository)



Switch to the source control view and you can also see an error message:



Solutions are as follows:

  1. useCtrl+Shift+POpen the command panel and entersetting, the choice ofPreferences: Open Settings (JSON):

  2. addgit.pathThe directory is on the current computergit.exeDirectory, as shown in the figure:

  3. Save and re-open the Git repository with VS Code:

2. Perform basic operations on the local library

2.1. Modify files

Modify the local repository to create a new onetest.cFile and add a piece of code as you can seeVS Code automatically displays the changes:

2.2. Temporary modification

Use the command to temporarily use the current modification on the command lineGit add < filename >, but in VS Code, you can operate graphically and see the contents of the current staging area and modification area:

2.3. Commit updates to the local library

Also, use the command by submitting the staging contents to the local library from the command lineGit commit -mIn VS Code, you can operate graphically:



After clicking the submit button, a dialog box for input submission information will pop up. You can enter the submission information:



Contents of the modification area and temporary storage area are cleared, as shown in the figure:

3. Install Git extensions to add more features

VS Code’s internal Git integration only supports some basic operations. Git extensions can be installed to support more functions, such asgit-extension-packThis extension:

Search in VS Code extensiongit, select the extension and install:

3.1. View the commit history

3.2. Check the status of the current content at any time

3.3. Check warehouse status at any time

3.4. View file differences



3.5. Switch the current branch

3.6. View differences between branches and merge branches



You can see the result as follows:



Merge branches to master after checking file differences:



Open command line inputgit merge:



Select the branch to merge into the current branch:



If the merge fails automatically, the following screen appears:



Modify conflict directly in the editor manually, after modification can be saved:

4. Remote library operations

Note: If the local repository is cloned or the remote repository address has been manually added, push and pull operations can be performed directly.

4.1. Create a new remote library on Github

4.2. Adding a remote library

useCtrl+Shift+POpen the command panel and entergit add, the choice ofAdding remote Libraries:



Fill in theAlias for the remote library address:



Fill in the remote library address:

4.3. Push content to remote libraries





5. More Git operations

So far, the basic operation of using VS Code with Git to do some local and remote libraries is covered.VS Code’s built-in Git extension also has many commands to execute, such as: initialize the local library, clone the remote library, pull the remote library content, and so onCtrl+Shift+POpen the command panel and entergitIf you are interested, you can try it yourself: