Skip to main content

Command Palette

Search for a command to run...

How to Use Version Control with Git

Updated
4 min read
How to Use Version Control with Git

Introduction

Version control is an essential tool in modern software development, and Git is one of the most widely used version control systems. GitHub, a platform built around Git, allows developers to collaborate on projects, track changes, and manage source code efficiently. In this article, we'll dive into the basics of Git and GitHub, showing you how to set up a repository, track changes, and collaborate with others. Let's get started!

Why Use Version Control?

Before we jump into Git and GitHub, let’s quickly recap why version control is important:

  • Track changes: Version control lets you keep a history of all changes made to your codebase. You can roll back to previous versions if something goes wrong.

  • Collaboration: Multiple developers can work on the same project simultaneously, and Git helps merge their work seamlessly.

  • Backup: Code is saved in a repository (repo) either locally or in the cloud (via GitHub), ensuring you won’t lose your work.

Getting Started with Git

Step 1: Installing Git

To start using Git, you need to install it on your machine. Head to Git's official website and download the installer for your operating system (Windows, macOS, or Linux).

After installation, verify it by running the following command in your terminal:

git --version

Step 2: Initializing a Git Repository

Once Git is installed, you can initialize version control for your project.

  1. Navigate to your project folder using the terminal.

     cd your-project-folder
    
  2. Initialize a new Git repository.

     git init
    

    This creates a .git folder in your project, which tracks your code changes.

Step 3: Staging and Committing Changes

Git allows you to stage changes before committing them, ensuring you review and select only the changes you want to include.

  1. Check the status of your files:

     git status
    
  2. Stage a file for commit:

     git add <filename>
    

    To stage all files, use:

     git add .
    
  3. Commit the changes with a message:

     git commit -m "Initial commit"
    

Step 4: Checking Git History

To view your commit history:

git log

This command will display a list of commits, their unique identifiers (hashes), and messages.

Using GitHub for Collaboration

Now that you have Git set up, let’s push your code to GitHub so you can collaborate with others and maintain a backup of your work.

Step 1: Create a GitHub Repository

  1. Log into your GitHub account.

  2. Click on the New button to create a new repository.

  3. Give your repo a name, and decide whether it should be public or private.

  4. Click Create repository.

You need to connect your local Git repository to GitHub so that you can push your code online.

  1. Copy the remote URL from your new GitHub repository.

  2. In your terminal, link the remote repo:

     git remote add origin https://github.com/your-username/your-repo.git
    

Step 3: Push Your Code to GitHub

To upload your code to GitHub, use the following commands:

git branch -M main
git push -u origin main

This pushes your local code to the remote repository on GitHub.

Collaborating with Others on GitHub

GitHub enables developers to work on the same project through branches, pull requests, and merges. Here's how:

Step 1: Creating a Branch

When collaborating, it's best to work on a separate branch instead of directly making changes to the main branch.

git checkout -b new-feature

Step 2: Making Changes and Pushing Your Branch

After making changes on your branch, commit and push the code:

git add .
git commit -m "Added new feature"
git push origin new-feature

Step 3: Creating a Pull Request

Once you're done with your changes, create a pull request on GitHub. A pull request allows team members to review your changes before merging them into the main branch.

  1. Go to your repository on GitHub.

  2. Click the Pull Requests tab and create a new pull request.

  3. Select your branch and the main branch to compare, then submit the pull request for review.

Step 4: Merging the Branch

After the review, you can merge the changes into the main branch either directly or after resolving any conflicts.

Common Git Commands

Here’s a quick reference to some essential Git commands:

  • Clone a repo:

      git clone https://github.com/username/repo.git
    
  • Check the status of your repo:

      git status
    
  • View your commit history:

      git log
    
  • Switch between branches:

      git checkout <branch-name>
    
  • Merge a branch into the current branch:

      git merge <branch-name>
    

Summary

Mastering Git and GitHub is essential for any developer. It helps you manage your code efficiently, collaborate with others, and keep a backup of your work. Whether you’re working on a solo project or a large team, Git enables seamless collaboration, ensuring that your code remains clean and organized. With GitHub’s additional features like pull requests and issue tracking, your development workflow becomes more streamlined and transparent.

By incorporating version control into your development process, you'll save time, reduce errors, and have better control over your project’s lifecycle.

Back-end

Part 1 of 1