Git and GitHub: A Guide for VS Code Users

Git and GitHub: A Guide for VS Code Users

Git is a version control system that enables you to manage the changes made to your code, track the history of those changes, and collaborate with others on projects.

GitHub is a web-based platform that provides Git hosting, as well as additional features for project management, bug tracking, and more.

In this guide, I'll walk you through the steps for using Git and GitHub with Visual Studio Code (VS Code), a popular code editor.

Step 1: Install Git

To use Git, you'll need to have it installed on your computer. If you don't already have it installed, you can download it from the official Git website. Once you've installed Git, you'll need to open a terminal window and enter the following command to check if it's installed properly:

git --version

Step 2: Install the Git extension for Visual Studio Code

The next step is to install the Git extension for Visual Studio Code. You can do this by opening the Extensions panel (View > Extensions), searching for "Git", and clicking the "Install" button next to the Git extension.

Step 3: Clone a GitHub repository

To clone a GitHub repository, you'll need to have a GitHub account and have the repository URL. If you don't already have a repository, you can create a new one on GitHub.

Once you have the repository URL, you can clone it to your local machine by opening a terminal window in VS Code (View > Terminal), and entering the following command:

git clone https://github.com/user/repo.git

Replace https://github.com/user/repo.git with the URL of the repository you want to clone.

Step 4: Make changes and add/commit

Now that you have the repository cloned to your local machine, you can start making changes to the code. Open the repository in VS Code, and make the changes you want. Once you're done, you can use the terminal window to the stage and commit the changes.

To stage changes in the terminal window, use the following command:

git add <file name>

To commit the changes in the terminal window, use the following command:

git commit -m "commit message"

Step 5: Push the changes to GitHub

To push the changes you made to the GitHub repository, you'll need to enter the following commands in the terminal window:

git push origin main

Replace origin with the name of the remote repository, and main with the name of the branch, you want to push to.

Git Commands Summary

Here is a summary of some common Git commands that you can use with the Git extension for Visual Studio Code or in the terminal:

  • git clone: This command is used to clone a remote repository to your local machine. The syntax is git clone <repository URL>.

  • git init: This command is used to initialize a new Git repository in your current directory.

  • git status: This command is used to view the current status of your repository, including the files that have been modified, staged, and committed.

  • git add: This command is used to stage changes to a file. The syntax is git add <file name>.

  • git commit: This command is used to commit the staged changes to the local repository. The syntax is git commit -m "commit message".

  • git push: This command is used to push the committed changes to a remote repository. The syntax is git push <remote name> <branch name>.

  • git pull: This command is used to retrieve changes from a remote repository and merge them into the local repository. The syntax is git pull <remote name> <branch name>.

  • git branch: This command is used to manage branches in your Git repository. You can use the git branch command to list the branches in your repository, create a new branch, or switch between branches.

These are just a few of the many Git commands available. By using Git and the Git extension for Visual Studio Code, you can manage your code repositories with ease and collaborate with others on your projects.

Visual Studio Code - Source Control Plugin

Visual Studio Code has a built-in source control plugin that integrates with Git, providing an easy way to manage changes to your code. Here are the steps to use the source control plugin in Visual Studio Code:

  1. Open the Source Control panel by clicking on the Source Control icon in the Activity Bar on the left side of the editor or by going to View > Source Control.

  2. In the Source Control panel, you'll see a list of the changes you've made to your code. You can stage changes by clicking the "+" icon next to each change, or you can stage all changes by clicking the "+" icon at the top of the panel.

  3. Once you've staged the changes, you can enter a commit message in the text box at the top of the panel and click the checkmark icon to commit the changes.

  4. To push your changes to GitHub, you can click the ... icon at the top of the panel and select "Push".

  5. You can also view the history of your changes by clicking the history icon in the Source Control panel. This will show you a list of all the commits you've made, including the date, author, and message for each commit.

The source control plugin in Visual Studio Code provides a convenient way to manage your Git repositories and keep track of changes to your code. By using the plugin, you can focus on writing code and leave the source control management to Visual Studio Code.

In conclusion, Git and GitHub are powerful tools for managing and collaborating on code projects. By following the steps outlined in this guide, you can start using Git and GitHub with Visual Studio Code and take your coding projects to the next level.

Did you find this article valuable?

Support Esteban Moreno - DevOps Portfolio by becoming a sponsor. Any amount is appreciated!