Skip to article frontmatterSkip to article content

Practical: Version Control System

In this practical, you will gain hands-on experience with Git, a powerful version control system, and learn how to integrate it with Visual Studio Code (VS Code). Both VS Code and equivalent command-line instructions are provided for each step. By the end of this practical, you will be able to:

These skills will help you manage your project files efficiently and collaborate effectively with others.

Open VS Code

Initialize a Git Repository

You can initialize a Git repository for the folder MLE5219 in VS Code or using the command line.

VS Code
Command Line
  • Go to the Source Control view by clicking on the Source Control icon in the Activity Bar on the side of the window.
  • Click on Initialize Repository.

Configure Git Username and Email

If it is your first time using Git, you need to configure your username and email address. This information will be used to identify your commits.

VS Code
Command Line
  • Open the terminal in VS Code:
    • Click on Terminal in the top menu.
    • Select New Terminal.
  • Run the following commands:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"

Commit and Push Changes

After making changes to your project files, you need to commit and push the changes to the remote repository. You can do this in VS Code or using the command line. For example, we can create a new file named README.md in the MLE5219 folder.

Example: Adding a README.md File

Stage Your Changes

After you have made changes to your files, you need to stage the changes before committing them. Staged changes mean that you have marked the files to be included in the next commit. You can also just commit all changes at once, see the next step.

VS Code
Command Line
  • In the Source Control view, click on the + icon next to the files you want to stage.

Commit Your Changes

Then, you need to commit your changes with a commit message. A commit message is a brief description of the changes you have made.

VS Code
Command Line
  • After staging your changes, enter a commit message in the message box at the top.
  • Click the checkmark icon to commit.

Connect to a Remote Repository

After committing your changes, you need to connect your local repository to a remote repository. You can do this in VS Code or using the command line. Here we will use a GitHub repository as remote. A remote repository URL on GitHub looks like https://github.com/user/repo.git.

If you don’t have a remote repository, you can either create one on GitHub and then connect your local repository to it or use VS Code to publish as a new repository to GitHub.

VS Code
Command Line
  • Click on the ... icon in the Source Control view.
  • Select Remote > Add Remote.
  • Enter the remote repository URL.

Push Your Changes

After connecting to a remote repository, you can push your changes to the remote repository.

VS Code
Command Line

You can just click the Sync changes in the Source Control panel to push your changes if you’ve commited all changes.

Or you can follow these steps:

  • Click on the ... icon in the Source Control view.
  • Select Push.

Pull Changes from Remote

If you are working with others on the same project, you may need to pull changes from the remote repository.

VS Code
Command Line
  • Click on the ... icon in the Source Control view.
  • Select Pull.

Clone a Remote Repository

If you want to work on an existing project, you can clone a remote repository to your local machine. You can do this in VS Code or using the command line.

VS Code
Command Line
  • Open the Source Control view.
  • Click Clone Repository.
  • Enter the remote repository URL.
  • Choose a folder to clone the repository into.

After cloning the repository, you can open it in VS Code and start working on it.

Create and Switch Branches

Branches are used to work on different features or versions of a project. You can create and switch branches in VS Code or using the command line.

Create a New Branch

Your current branch is usually main or master. First we will create a new branch named development. On VS Code, you can see the current branch name in the bottom left corner of the window.

VS Code
Command Line
  • Click on the branch name in the bottom left corner of the window.
  • Select Create new branch.
  • Enter the new branch name, here we use development.

Switch Branches

You can switch between branches to work on different features or versions of a project. In VS Code, you can change the branch by clicking on the branch name in the bottom left corner of the window.

VS Code
Command Line
  • Click on the branch name in the bottom left corner of the window.
  • Select the branch you want to switch to.

Merge Branches

After working on different branches, you may need to merge the changes from one branch to another. You can merge branches in VS Code or using the command line. Make sure you are currently on the branch you want to merge into.

VS Code
Command Line
  • Click on the branch name in the bottom left corner of the window.
  • Select the branch you want to merge into.
  • Click on the ... icon next to the branch name.
  • Select Pull from....
  • Choose the branch you want to pull changes from.

Resolve Merge Conflicts

When you merge branches with conflicting changes, you may encounter merge conflicts. Merge conflicts occur when Git cannot automatically merge the changes and requires manual intervention to resolve the conflicts. You can resolve merge conflicts in VS Code or using the command line.

Example of Resolving a Conflict

Here we will create a conflict by making changes to the same line in two different branches. Then we will merge the branches and resolve the conflict. Previously we have created the development branch. Now we will create a new branch named feature and make changes to the README.md file.

Add the following content to the README.md file in the feature branch:

# MLE5219 Project
This is the README file for the MLE5219 project.
This is a new feature added to the README file.

Then commit and push the changes to the feature branch. Now switch to the development branch and make changes to the README.md file:

# MLE5219 Project
This is the README file for the MLE5219 project.
This is a new feature added to the README file in the development branch.

Commit and push the changes to the development branch. Now merge the feature branch into the development branch. You will see a conflict in the README.md file. Resolve the conflict by choosing the appropriate changes and commit the resolved file.

VS Code
Command Line
  • Merge the branches:
    • Click on the branch name in the bottom left corner of the window.
    • Select the branch you want to merge into. Here we use development.
    • Click on the ... icon next to the branch name.
    • Select Pull from....
    • Choose the branch you want to pull changes from. Here we use feature.
  • VS Code will show the conflicts in the editor.
  • Choose the appropriate option to resolve the conflict:
    • Accept Current Change
    • Accept Incoming Change
    • Accept Both Changes
    • Compare Changes
  • After resolving the conflicts, stage the resolved files.
  • Commit the changes.

View Git History

You can view the history of commits and changes in your Git repository. You can do this in VS Code or using the command line.

VS Code
Command Line
  • Install the Git Graph extension:
    • Go to the Extensions view.
    • Search for Git History.
    • Install the extension.
  • View the history:
    • Right-click on a file.
    • Select Git: View File History.

That’s it! You are now ready to use Git with VS Code.