Introduction to Version Control with Git

Using a Version Control System (VCS) like Git is essential for managing code changes and collaborating with team members. Git is the standard version control tool in modern software development, allowing you to track changes, revert to previous states, and collaborate efficiently with others. In this lesson, we’ll cover the basics of Git, how to install it, and a small test to ensure your setup is working correctly.

What is Git?

Git is a distributed version control system that allows multiple developers to work on a project simultaneously. Its branching and merging capabilities make it easy to experiment with new features and roll back changes if necessary. Git is the most widely used VCS in the world and forms the backbone of many collaboration platforms.

How to Install Git

Installing Git on Windows

  1. Download the Git installer from the Git website.
  2. Run the installer and follow the prompts, selecting the default options.
  3. Verify the installation by opening Command Prompt and typing:
    git --version
    

Installing Git on macOS

  1. Open the Terminal application.
  2. If you have Homebrew installed, run:
    brew install git
    
    Alternatively, you can download the installer from the Git website.
  3. Verify the installation by typing:
    git --version
    

Installing Git on Linux

  1. Open your terminal.

  2. Install Git using your distribution’s package manager:

    • For Debian/Ubuntu-based distributions:
      sudo apt-get install git
      
    • For Fedora-based distributions:
      sudo dnf install git
      
    • For Arch Linux:
      sudo pacman -S git
      
  3. Verify the installation by typing:

    git --version
    

Git Providers

Here are a few popular Git providers that offer cloud-based repositories and additional features:

GitHub

A cloud-based platform that uses Git for version control. GitHub is popular for its ease of use and features like pull requests and issue tracking, making it ideal for collaboration.

GitLab

Another platform for hosting Git repositories, GitLab provides built-in CI/CD capabilities, issue tracking, and more.

Bitbucket

Bitbucket supports Git and Mercurial repositories, with integrations into Atlassian tools like Jira, making it a popular choice for enterprise environments.

Setting Up and Testing Git

Configure Git

After installing Git, set up your username and email:

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

Create a New Repository

  1. Create a new directory and initialize it as a Git repository:
    mkdir my-test-repo
    cd my-test-repo
    git init
    

Add a File

  1. Create a new file and add it to the repository:
    echo "# My Test Repository" > README.md
    git add README.md
    git commit -m "Initial commit"
    

Push to a Remote Repository

  1. If you have a GitHub account, create a new repository on GitHub and push your local repository to GitHub:
    git remote add origin https://github.com/yourusername/my-test-repo.git
    git push -u origin master
    

Clone a Repository

  1. Clone an existing repository to your local machine:
    git clone https://github.com/yourusername/my-test-repo.git
    

Make Changes and Commit

  1. Edit the README.md file and add a new commit:
    echo "This is a test repository." >> README.md
    git add README.md
    git commit -m "Added a description"
    git push
    

For more learning resources, check out these links:

Conclusion

Git is a powerful tool for modern software development, enabling version control and collaboration. By following this guide, you can install Git, configure it, and perform basic operations to manage your code effectively. Using platforms like GitHub, GitLab, or Bitbucket further enhances collaboration and integrates well with other development tools.


Multiple Choice Questions

Test your knowledge with the following questions:

  1. Which of these is NOT a Git provider?

    • A) GitHub
    • B) GitLab
    • C) Bitbucket
    • D) Google Drive
  2. What command initializes a new Git repository?

    • A) git clone
    • B) git init
    • C) git start
    • D) git new
  3. How do you set your Git username?

    • A) git set username "Your Name"
    • B) git config --global user.name "Your Name"
    • C) git user.name "Your Name"
    • D) git set --name "Your Name"
  4. What command is used to check the installed version of Git?

    • A) git check
    • B) git version
    • C) git --version
    • D) git status