🎉 Announcing our new Equinix Sydney data centre SY5!

Setting Up Version Control

Version control is a critical aspect of modern software development, allowing developers to track changes, collaborate efficiently, and manage the history of a project. Git is one of the most widely used version control systems, and it is often paired with platforms like GitHub and GitLab to host repositories and collaborate on projects. In this article, we’ll explore how to set up version control using Git and connect it to GitHub or GitLab, providing you with a powerful workflow for managing your project’s source code.


What is Version Control?

Version control refers to the practice of tracking and managing changes to software code. This system allows developers to work on projects collaboratively while maintaining a history of all changes made. If any issues arise, developers can roll back to previous versions of their code, compare different versions, and merge changes made by different contributors.

Git is a distributed version control system that enables developers to keep track of changes to files locally and collaborate with others using remote repositories hosted on platforms like GitHub and GitLab.


Setting Up Git Locally

Before you can start using Git for version control, you need to set it up on your local machine.

1. Install Git

To get started with Git, you’ll first need to install it on your local machine.

  • Windows: Download the Git installer from the official Git website, then run the installation file and follow the instructions.

  • macOS: You can install Git using Homebrew by running brew install git in the terminal, or you can download the installer from the Git website.

  • Linux: Most Linux distributions come with Git pre-installed. If not, you can install it via your package manager, for example, sudo apt install git on Ubuntu.

2. Configure Git

After installing Git, you need to configure your user name and email address, which will be associated with your commits.

Open the terminal and run the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

These settings are saved globally, so you don’t need to configure them again for each repository.

3. Initialize a Git Repository

Once Git is installed and configured, you can start tracking changes in a project by initializing a Git repository. In your project directory, open the terminal and run:

git init

This command creates a .git directory in your project, which is used by Git to track changes.


Setting Up a Remote Repository (GitHub or GitLab)

To collaborate with others or back up your code online, you'll need to use a remote Git repository. GitHub and GitLab are two popular platforms for hosting Git repositories. Here, we'll cover the basics of setting up a repository on both platforms.

1. Create a GitHub or GitLab Account

First, sign up for a free account on either GitHub or GitLab, depending on your preference.

2. Create a New Repository

  • GitHub:

    1. After logging in, click on the "+" icon in the top-right corner of the GitHub homepage and select New repository.

    2. Name your repository, add a description, and choose whether to make it public or private.

    3. You can initialize the repository with a README.md file, but if you already have a local project, you can skip this.

  • GitLab:

    1. Log into your GitLab account and click on the New project button.

    2. Choose a project name, and optionally, a description and visibility level.

    3. Click Create project to finish.

3. Connect Your Local Repository to the Remote Repository

Once your remote repository is created, you need to connect your local Git repository to it. Copy the URL of your repository from GitHub or GitLab (it should look like https://github.com/username/repository-name.git or https://gitlab.com/username/repository-name.git).

In the terminal, navigate to your local project directory and run:

git remote add origin https://github.com/username/repository-name.git

This command links your local repository to the remote one, allowing you to push changes to GitHub or GitLab.

4. Push Your Local Files to the Remote Repository

Now that your local and remote repositories are connected, you can upload your code. Start by adding your files to Git’s staging area:

git add .

This command stages all files in the project directory for commit. Next, commit your changes:

git commit -m "Initial commit"

This records the changes in your local Git repository. Finally, push your changes to the remote repository:

git push -u origin master

The -u flag sets the remote repository as the default for future pushes, so you only need to run git push next time.


Working with Git (GitHub/GitLab) Locally and Remotely

Once your repository is set up, you can begin using Git to manage your project. Here are some common tasks you’ll use while working with Git.

1. Cloning a Repository

If you want to work on an existing repository, you can clone it to your local machine. Run:

git clone https://github.com/username/repository-name.git

This command copies the entire repository (including all its history) to your local machine, allowing you to work with the project files.

2. Pulling Changes from the Remote Repository

When others have made changes to the project or you need to update your local repository with the latest changes, you can use the git pull command:

git pull origin master

This fetches and merges changes from the remote repository to your local copy.

3. Branching and Merging

For collaboration, you may want to work on features or bug fixes without affecting the main codebase. This is done through branches.

To create a new branch:

git checkout -b new-feature

This creates and switches to the new branch. After making changes, you can commit them:

git add .
git commit -m "Added a new feature"

Then, push your branch to the remote repository:

git push origin new-feature

To merge a branch into the master branch, first, switch to the master branch:

git checkout master

Then, merge the feature branch:

git merge new-feature

Version control is essential for modern web development, and Git is the most powerful tool available for managing your project’s source code. By setting up Git locally and connecting it to platforms like GitHub or GitLab, you can easily track changes, collaborate with other developers, and maintain a clean history of your project.

Last updated

Was this helpful?