Overview

>

Version-control

Version Control with Git: Basics and Best Practices


In this module, you’ll learn the fundamentals of version control using Git. We’ll cover what version control is, why Git is the most popular system for it, and how to use Git to manage your projects effectively. By the end of this module, you’ll be comfortable with initializing repositories, making commits, branching, and collaborating with others using Git and GitHub.

Estimated reading time: ~25 minutes


Table of Contents

  1. Introduction to Version Control
    • What is Version Control?
    • Why Use Git?
  2. Setting Up Git
    • Installing Git
    • Configuring Git
  3. Basic Git Commands
    • Initializing a Repository
    • Staging and Committing Changes
    • Checking the Status
    • Viewing Commit History
  4. Working with Branches
    • Creating and Switching Branches
    • Merging Branches
    • Resolving Merge Conflicts
  5. Collaborating with Git and GitHub
    • Setting Up a GitHub Repository
    • Pushing and Pulling Changes
    • Working with Pull Requests
  6. Best Practices
  7. Conclusion
  8. Additional Resources

Introduction to Version Control

What is Version Control?

Version control is a system that records changes to files over time so that you can recall specific versions later. It allows multiple people to work on a project simultaneously without overwriting each other’s changes.

Benefits of Version Control:

  • History Tracking: Keep a history of changes, making it easy to revert to previous versions.
  • Collaboration: Multiple developers can work on the same project simultaneously.
  • Branching and Merging: Isolate changes in branches and merge them back when ready.
  • Backup: Acts as a backup of your codebase.

Why Use Git?

Git is the most widely used modern version control system in the world today. Here are some reasons why Git is popular:

  • Distributed System: Every developer has a full copy of the repository.
  • Performance: Git is fast and efficient.
  • Branching Model: Lightweight branching and merging.
  • Open Source: Free to use and has a large community.
  • Integration with Platforms: Works seamlessly with platforms like GitHub, GitLab, and Bitbucket.

Setting Up Git

Installing Git

For Windows:

  • Download the Git installer from the official website.
  • Run the installer and follow the on-screen instructions.

For macOS:

For Linux:

  • Use your distribution’s package manager. For example, on Ubuntu:

    sudo apt-get update
    sudo apt-get install git
    
    

Configuring Git

After installing Git, you need to set up your username and email. These will be associated with your commits.

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

You can verify your configuration:

git config --list


Basic Git Commands

Initializing a Repository

To start tracking a project with Git, you need to initialize a repository.

  1. Navigate to your project directory:

    cd path/to/your/project
    
    
  2. Initialize Git:

    git init
    
    

    This command creates a hidden .git directory that tracks changes.

Staging and Committing Changes

Staging Changes:

  • To stage all changes:

    git add .
    
    
  • To stage specific files:

    git add filename.txt
    
    

Committing Changes:

  • Commit staged changes with a message:

    git commit -m "Your commit message"
    
    

Commit Message Guidelines:

  • Use the imperative mood (e.g., “Add feature” instead of “Added feature”).
  • Keep it concise but descriptive.
  • Example: "Implement user authentication"

Checking the Status

To see the status of your repository:

git status

This command shows:

  • Files that have been modified.
  • Files staged for commit.
  • Untracked files.

Viewing Commit History

To view the commit history:

git log

For a condensed view:

git log --oneline


Working with Branches

Creating and Switching Branches

Branches allow you to work on different versions of a project simultaneously.

Create a new branch:

git branch feature-branch

Switch to a branch:

git checkout feature-branch

Create and switch in one command:

git checkout -b feature-branch

Merging Branches

Once you’ve made changes in your feature branch and want to integrate them into the main branch:

  1. Switch to the main branch:

    git checkout main
    
    
  2. Merge the feature branch:

    git merge feature-branch
    
    

Resolving Merge Conflicts

A merge conflict occurs when changes in two branches conflict.

Steps to resolve:

  1. Git will indicate which files have conflicts.

  2. Open the conflicted files; you’ll see markers like:

    <<<<<<< HEAD
    Current changes
    =======
    Incoming changes
    >>>>>>> feature-branch
    
    
  3. Manually edit the file to resolve conflicts.

  4. After resolving, add the files:

    git add conflicted-file.txt
    
    
  5. Commit the merge:

    git commit
    
    

Collaborating with Git and GitHub

Setting Up a GitHub Repository

  1. Create a GitHub Account:

  2. Create a New Repository:

    • Click on the “New” button.
    • Enter a repository name and description.
    • Choose to make it public or private.
  3. Link Your Local Repository to GitHub:

    In your local project directory:

    git remote add origin <https://github.com/yourusername/your-repo.git>
    
    

Pushing and Pulling Changes

Push Local Changes to GitHub:

  • Push your commits to the remote repository:

    git push -u origin main
    
    

    The -u flag sets the remote origin as the upstream for the main branch.

Pull Changes from GitHub:

  • To update your local repository with changes from GitHub:

    git pull origin main
    
    

Working with Pull Requests

A Pull Request (PR) is a way to propose changes to a repository.

Steps to create a PR:

  1. Push Your Feature Branch to GitHub:

    git push origin feature-branch
    
    
  2. Create a Pull Request on GitHub:

    • Navigate to your repository on GitHub.
    • Click on “Compare & pull request”.
    • Add a title and description.
    • Assign reviewers if needed.
  3. Review and Merge:

    • Reviewers can comment on your code.
    • After approval, merge the PR into the main branch.

Best Practices

  1. Commit Often:
    • Make frequent, small commits.
    • Helps in tracking changes and isolating issues.
  2. Write Meaningful Commit Messages:
    • Clearly describe what changes the commit introduces.
  3. Use Branches:
    • Keep the main branch stable.
    • Use feature branches for development.
  4. Pull Before Pushing:
    • Always pull changes from the remote repository before pushing.
    • Reduces the risk of conflicts.
  5. Resolve Conflicts Promptly:
    • Address merge conflicts as soon as they arise.
  6. Collaborate Through Pull Requests:
    • Use PRs for code reviews and discussions.
  7. Ignore Unnecessary Files:
    • Use a .gitignore file to exclude files like node_modules, .env, and others.

      # .gitignore
      node_modules/
      .env
      dist/
      
      
  8. Secure Your Repositories:
    • Do not commit sensitive information.
    • Use environment variables or configuration files that are not tracked.

Conclusion

Version control is an essential tool for any developer. Git, combined with platforms like GitHub, provides a powerful workflow for tracking changes, collaborating with others, and managing codebases effectively. By mastering the basics covered in this module, you’re well on your way to becoming proficient in using Git for your projects.


Additional Resources


🎉 Congratulations! You’ve completed the Version Control with Git module. Continue practicing by applying these concepts to your projects and collaborating with others.