Mastering Git: A Beginner’s Guide with Day-by-Day Lessons

Mastering Git: A Beginner’s Guide with Day-by-Day Lessons


Introduction
Git is an essential tool for modern developers. This guide documents my learning journey and serves as a resource for anyone starting with Git. Here, I’ve detailed the daily steps, commands, and exercises I followed to master Git, along with explanations and extra content to deepen your understanding.


Day 1: Introduction to Git

What is Git?

Git is a distributed version control system that tracks changes in your codebase, enabling collaboration and efficient management of your projects.

Key Features of Git

  • Tracks changes across versions.

  • Works offline, allowing local version control.

  • Supports branching and merging for independent and collaborative development.

  • Integrates seamlessly with platforms like GitHub, GitLab, or Bitbucket.


Basic Concepts in Git

  • Repository (Repo): A repository is where your project's files and their history are stored. It can be local (on your machine) or remote (on a server like GitHub, GitLab, etc.).

  • Commit: A snapshot of your code at a particular point in time.

  • Branch: A separate line of development, allowing you to work on features or fixes independently.

  • Merge: Combining changes from different branches into one.

  • Staging Area: A temporary area where changes are prepared before committing.

  • HEAD: The current branch or commit you are working on.

Step-by-Step Guide to Set Up Git

Frequently Used Git Commands

Here are some essential Git commands to get started:

CommandDescription
git statusShows the current state of your repository.
git add <file>Stages a file for commit. Use git add . to stage all files.
git commit -m "message"Creates a commit with a descriptive message.
git logDisplays the commit history.
git branchLists all branches in the repository.
git checkout <branch>Switches to the specified branch.
git merge <branch>Merges the specified branch into the current branch.
git pullUpdates your local repository with changes from the remote.
git pushSends your commits to the remote repository.
  1. Install Git
    On Ubuntu or WSL (Windows Subsystem for Linux):

     sudo apt update  
     sudo apt install git -y  
     git --version
    

    Verify installation. The output should display something like git version 2.x.x.

  2. Configure Git
    Set up your name and email for commits:

     git config --global user.name "Your Name"  
     git config --global user.email "your.email@example.com"  
     git config --list
    
  3. Generate SSH Keys
    For secure interaction with platforms like GitHub:

     ssh-keygen -t rsa -b 4096 -C "your.email@example.com"  
     eval "$(ssh-agent -s)"  
     ssh-add ~/.ssh/id_rsa  
     cat ~/.ssh/id_rsa.pub
    

    Copy the public key (id_rsa.pub) and add it to your GitHub account under Settings > SSH and GPG Keys.

  4. Create Your First Repository
    On GitHub:

    • Create a new repository (e.g., my-first-repo).

    • Clone it locally:

        git clone git@github.com:your-username/my-first-repo.git  
        cd my-first-repo
      

Hands-On Exercise for Day 1

  1. Create a new repository named learning-git on GitHub.

  2. Locally, create a file notes.txt with the content:

     echo "Git is a distributed version control system." > notes.txt  
     git add notes.txt  
     git commit -m "Added notes.txt with Git basics"  
     git push origin main
    
  3. Verify the changes are reflected on GitHub.

Outcome for Day 1:

  • Set up Git on your environment.

  • Configured SSH keys.

  • Created, cloned, and pushed changes to a GitHub repository.


Day 2: Core Git Commands and Workflow

1. Git Workflow Overview

Git operates through three main stages:

  1. Working Directory: Files you’re actively working on.

  2. Staging Area: Changes added using git add, ready to commit.

  3. Repository: The commit history of your project.

Key Commands Recap:

git status          # Check repository state  
git add <file>      # Stage changes  
git commit -m "Message"  # Save changes to history  
git push origin main  # Upload changes to remote repository

2. Branching and Merging

Branches allow independent work on features or fixes.

  • Create a new branch:

      git branch feature-branch
    
  • Switch to the branch:

      git switch feature-branch
    
  • Merge changes back to main:

      git switch main  
      git merge feature-branch
    

Hands-On Exercise: Branching and Merging

  1. Create and switch to a branch named add-welcome-message.

  2. Add a welcome message to README.md and commit changes.

  3. Merge the branch back to main.

  4. Optionally delete the branch:

     git branch -d add-welcome-message
    

3. Resolving Merge Conflicts

Conflicts occur when two branches modify the same lines in a file.

Steps to Resolve a Conflict

  1. Simulate a conflict:

    • Add different lines to the same file (notes.txt) on two branches.
  2. Attempt to merge the branches:

     git merge conflict-branch
    
  3. Resolve the conflict manually by editing the file to keep the desired content.

  4. Add the resolved file and commit:

     git add notes.txt  
     git commit -m "Resolved merge conflict"
    

4. Working with Remote Repositories

Remote repositories enable collaboration.

  • Fetch updates without merging:

      git fetch origin
    
  • Pull updates and merge:

      git pull origin main
    
  • Push changes to remote:

      git push origin <branch>
    

Hands-On Exercise

  1. Simulate collaboration by modifying a file on GitHub directly.

  2. Pull the changes locally and merge them with your work.

  3. Push your final changes back to the repository.

Outcome for Day 2:

  • Learned branching, merging, and resolving conflicts.

  • Practiced collaboration using remote repositories.


Next Steps

Day 3 will focus on advanced Git workflows, including:

  • Using tags for releases.

  • Cherry-picking commits.

  • Interactive rebasing for cleaner commit history.