2025-02-14 Productivity, Programming

Introduction to Git & Version Control

By O. Wolfson

What is Version Control?

Version control is a system that records changes to files over time, allowing multiple people to collaborate efficiently, track modifications, and revert to previous versions when needed. It is crucial in production environments where maintaining a reliable history of changes ensures stability, accountability, and the ability to fix issues quickly.

Without version control, managing multiple versions of a project can be chaotic, leading to lost progress, overwritten work, and difficulty in tracking changes. Version control systems (VCS) prevent these issues by maintaining a structured history of changes.

Git vs. GitHub vs. GitLab vs. Bitbucket

While Git is a distributed version control system, platforms like GitHub, GitLab, and Bitbucket provide hosting services and collaboration features built around Git. Here’s how they differ:

  • Git: A local tool for tracking changes in source code.
  • GitHub: A cloud-based Git repository hosting service with additional features like issue tracking, CI/CD, and collaboration tools.
  • GitLab: Similar to GitHub but offers more built-in DevOps features and self-hosting options.
  • Bitbucket: A Git repository hosting service by Atlassian, known for strong integration with Jira and enterprise-focused tools.

When to Use Each:

  • Git: Always used for local version control, even if working alone.
  • GitHub: Best for open-source projects and general collaboration.
  • GitLab: Ideal for self-hosting and integrated DevOps workflows.
  • Bitbucket: Preferred for enterprises using Atlassian tools like Jira.

Centralized vs. Distributed Version Control

Version control systems fall into two categories:

  • Centralized VCS (CVCS): A single central server stores all versions of a project. Developers must connect to the server to retrieve and update files. Examples include Subversion (SVN) and Perforce.
  • Distributed VCS (DVCS): Each user has a full copy of the repository, allowing offline work and reducing reliance on a central server. Git and Mercurial are examples of DVCS.

Why Git Uses a Distributed Model

Git’s distributed nature provides several advantages:

  • No single point of failure: Every copy contains the full history.
  • Faster operations: Most actions are local, reducing network dependency.
  • Better collaboration: Developers can work independently and merge changes later.

Installing Git

To start using Git, install it on your operating system.

Windows

  1. Download the installer from git-scm.com.

  2. Run the installer and follow the setup instructions.

  3. Verify the installation:

    sh
    git --version
    

macOS

  1. Install Git using Homebrew:

    sh
    brew install git
    
  2. Verify the installation:

    sh
    git --version
    

Linux

  1. Install Git using the package manager:

    sh
    sudo apt install git  # Debian-based systems
    sudo dnf install git  # Fedora-based systems
    
  2. Verify the installation:

    sh
    git --version
    

Configuring Git

After installation, configure Git with your user information:

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

To check the current configuration:

sh
git config --list

Hands-on Exercise: Install and Configure Git

  1. Install Git following the instructions above.

  2. Set up your global Git configuration with your name and email.

  3. Generate SSH keys for GitHub authentication:

    SSH keys are cryptographic keys that allow secure authentication with remote servers like GitHub without using a password. Generate an SSH key using the following command:

    sh
    ssh-keygen -t ed25519 -C "your.email@example.com"
    
    • The tool will prompt you to enter a location to save the key. Press Enter to use the default location, which is ~/.ssh/id_ed25519.
    • After generating the key, you will have two files:
      • id_ed25519 (private key, never share this)
      • id_ed25519.pub (public key, used for authentication)
  4. Copy the public key:

    sh
    cat ~/.ssh/id_ed25519.pub
    

    This command displays the public key, which you need to add to GitHub.

  5. Add the key to GitHub:

    • Go to GitHub Settings
    • Navigate to SSH and GPG keys
    • Click New SSH key
    • Paste your public key into the provided field and save it.
  6. Test the connection:

    sh
    ssh -T git@github.com
    

    If successful, you’ll see a message like:

    text
    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
    

    This means the connection is working, but GitHub does not allow direct shell access like a traditional server. You can now securely push and pull code from GitHub using SSH authentication.

You are now ready to start using Git for version control!