Week 1: Introduction to Git

Week 1: Introduction to Git

Day 1: What is Version Control?

Version Control Systems (VCS): A Version Control System is a tool that helps you manage changes to source code over time. It allows multiple developers to work on a project simultaneously without overwriting each other's work, provides the ability to revert to previous versions, and helps track changes.

Benefits of Using a VCS:

  1. Collaboration: Multiple developers can work on the same project simultaneously.

  2. History: Keeps a history of all changes made to the code.

  3. Backup: Acts as a backup system for your code.

  4. Branching and Merging: Allows for experimental development and feature branching without affecting the main codebase.

  5. Blame Assignment: Identifies who made specific changes.

Popular VCS Options:

  • Git: The most widely used modern VCS.

  • SVN (Subversion): A centralized version control system.

  • Mercurial: Another distributed version control system, similar to Git.

Why Git?

  • Distributed: Every developer has a full copy of the repository, including the entire history.

  • Fast and Efficient: Git is designed to be fast and handle large projects efficiently.

  • Powerful Branching and Merging: Git has robust branching and merging capabilities.

  • Widely Used: Git has a large community and is widely adopted in the industry.

Detailed Explanation with Code

Getting Started with Git:

  1. Installing Git:

    • Windows: Download and install from git-scm .

    • MacOS: Install via Homebrew with brew install git.

    • Linux: Install via the package manager, e.g., sudo apt-get install git on Debian-based systems.

  2. Configuring Git: Once Git is installed, you need to configure it with your name and email. This information is used in every commit you make.

     #Set your username
     git config --global user.name "Your Name"
    
     #Set your email
     git config --global user.email "youremail@example.com"
    
     #Verify configuration
     git config --list
    
  3. Basic Git Commands:

    • Initialize a New Git Repository: This command creates a new Git repository in your project directory.

        git init
      
    • Configure Git: You’ve already seen the configuration commands above. Here they are again for reference.

        git config --global user.name "Your Name"
        git config --global user.email "youremail@example.com"
      
    • Help Command: This command shows help for Git commands.

        git help
      
    • Understanding the .git Directory: After initializing a Git repository, a .git directory is created. This directory contains all the necessary metadata for the repository, including the entire history of commits, branches, and configuration.

        ls -la .git
      

This will list the contents of the .git directory, which includes:

  • HEAD: A reference to the current commit.

  • config: Configuration file for the repository.

  • description: A description of the repository.

  • hooks/: Client-side scripts that Git executes for specific events.

  • info/: Additional information about the repository.

  • objects/: The object database storing all content.

  • refs/: References to commits, usually branches.

Example Workflow:

Let's create a new project and initialize it with Git.

    # Step 1: Create a new directory for your project
    mkdir my-git-project
    cd my-git-project

    # Step 2: Initialize a new Git repository
    git init

You should see output similar to this:

    Initialized empty Git repository in /path/to/my-git-project/.git/

Now, let's create a file and commit it.

    # Step 3: Create a new file
    echo "Hello, Git!" > hello.txt

    # Step 4: Check the status of your repository
    git status

The output will indicate that you have an untracked file:

    On branch master

    No commits yet

    Untracked files:
      (use "git add <file>..." to include in what will be committed)
          hello.txt

    nothing added to commit but untracked files present (use "git add" to track)
    # Step 5: Add the file to the staging area
    git add hello.txt

    # Step 6: Commit the file to the repository
    git commit -m "Initial commit"

The output will show that the file has been committed:

    [master (root-commit) d1e8d3b] Initial commit
     1 file changed, 1 insertion(+)
     create mode 100644 hello.txt

By the end of Day 1, we are able to understand the importance of version control, why Git is a popular choice, and how to set up and make their first commit with Git. This foundational knowledge will set the stage for more advanced topics in the coming days . So Stay tuned!