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:
- Initialize a Git repository
- Configure Git with your username and email
- Commit and push changes to a remote repository
- Pull changes from a remote repository
- Clone a remote repository
- Create and switch branches
- Resolve merge conflicts
- View Git history
These skills will help you manage your project files efficiently and collaborate effectively with others.
Open VS Code¶
- Launch Visual Studio Code on your computer.
- Click on
Filein the top menu. - Select
Open Folder.... - Navigate to and select the folder you want to work with. For example, you can create a new folder on your desktop named
MLE5219and then open it.
Initialize a Git Repository¶
You can initialize a Git repository for the folder MLE5219 in VS Code or using the 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.
- Open your terminal.
- Navigate to your project directory using
cdcommand. - Run the following command:
git init
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.
- Open the terminal in VS Code:
- Click on
Terminalin the top menu. - Select
New Terminal.
- Click on
- Run the following commands:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
- Open your 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¶
- In the Explorer view, click on the
New Fileicon. - Name the file
README.md. - Add some content to the file, for example:
# MLE5219 Project This is the README file for the MLE5219 project. - Save the 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.
- In the Source Control view, click on the
+icon next to the files you want to stage.
- Open your terminal.
- Run the following command to stage your changes:
git add .
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.
- After staging your changes, enter a commit message in the message box at the top.
- Click the checkmark icon to commit.
- Open your terminal.
- Run the following command to commit your changes:
git commit -m "Your commit message"
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.
- Click on the
...icon in the Source Control view. - Select
Remote>Add Remote. - Enter the remote repository URL.
- Open your terminal.
- Run the following command to add a remote repository:
git remote add origin <remote-repository-URL>
Push Your Changes¶
After connecting to a remote repository, you can push your changes to the remote repository.
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.
- Open your terminal.
- Run the following command to push your changes:
git push -u origin main
Pull Changes from Remote¶
If you are working with others on the same project, you may need to pull changes from the remote repository.
- Click on the
...icon in the Source Control view. - Select
Pull.
- Open your terminal.
- Run the following command to pull changes:
git 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.
- Open the Source Control view.
- Click Clone Repository.
- Enter the remote repository URL.
- Choose a folder to clone the repository into.
- Open your terminal.
- Navigate to the directory where you want to clone the repository using the
cdcommand. - Run the following command:
git clone <remote-repository-URL>
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.
- 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.
- Open your terminal.
- Run the following command to create a new branch, here we use
development:git checkout -b 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.
- Click on the branch name in the bottom left corner of the window.
- Select the branch you want to switch to.
- Open your terminal.
- Run the following command to switch branches:
git checkout branch-name
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.
- 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.
- Open your terminal.
- Run the following command to pull changes from another branch:
git pull origin branch-name
Comparison Between git pull, rebase, and merge
When working with branches in Git, you have several options to integrate changes from one branch into another. The three most common commands are git pull, git rebase, and git merge. Each command has its own use case and behavior.
| Command | Usage | Pros | Cons | When to Use |
|---|---|---|---|---|
git pull | git pull origin branch-name | Simple and straightforward | Can create unnecessary merge commits | Quickly update your branch with changes from a remote repository |
| Automatically fetches and merges changes | May lead to a cluttered commit history | |||
git rebase | git rebase branch-name | Creates a cleaner, linear commit history | More complex and risky, especially with conflicts | When you want a clean, linear commit history and are comfortable resolving conflicts |
| Easier to follow project history | Rewriting commit history can cause issues | |||
git merge | git merge branch-name | Preserves complete history of changes | Can create a cluttered commit history | When you want to preserve the history of both branches and understand the context of changes |
| Easier to understand context of changes | May require resolving merge conflicts |
Choosing the right command depends on your workflow and the project’s requirements. Understanding the differences and use cases will help you manage your branches more effectively.
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.
- 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.
- Merge the branches:
git pull origin feature - Open the conflicting files in your editor.
- Manually resolve the conflicts.
- Stage the resolved files:
git add . - Commit the changes:
git commit -m "Resolved merge conflicts"
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.
- 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.
- Open your terminal.
- Run the following command to view the git log:
git log - If you want to see a more detailed log, you can use:
git log --oneline --graph --all
That’s it! You are now ready to use Git with VS Code.