Git & GitHub Crash Course: Part 1

Git & GitHub Crash Course: Part 1

1. What is a Version Control System?

A Version Control System (VCS) is software that helps developers track and manage changes to their code. It enables collaboration, as multiple developers can work on the same project without overwriting each other’s work. The benefits are countless, from allowing rollbacks to creating separate branches for testing features.

Some popular VCSs include:

  • Git: The most widely used VCS, popular for its distributed system and speed.

  • Apache Subversion (SVN): A centralized VCS known for simplicity.

  • Piper: A Google-specific VCS designed for vast, collaborative projects.


2. Introduction to Git VCS

Git is a distributed version control system that tracks code changes locally, meaning every contributor has a complete version history on their device. Here’s how to get started:

Installation of Git CLI

  1. Visit Git’s official website and download the CLI.

  2. Follow the installation instructions specific to your OS.

Setting up Git Global Configuration

To use Git, start by setting up some global configurations:

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

Basic Git Commands

Here are some essential commands to know before diving deeper:

  • git --version: Check your installed Git version.

  • git help: Display available commands.


3. Version Controlling with Git

Once you have Git set up, let’s start version controlling a project!

Initializing a Git Project

Navigate to your project folder and initialize it as a Git repository:

bashCopy codegit init

Adding Files to VCS

Git only tracks files you explicitly add. Use the following commands:

  • To add a specific file:

      bashCopy codegit add <FILE_PATH>
    
  • To add all files in the current directory:

      bashCopy codegit add .
    

Committing Files

Each code snapshot in Git is a commit. Use:

bashCopy codegit commit -m "Your commit message"

Staging Area

Git uses a staging area as an intermediate state before committing files, allowing you to decide which changes go into the commit.

Logging Commit History

  • Full log history:

      bashCopy codegit log
    
  • Brief log history:

      bashCopy codegit log --oneline
    

4. Git VS GitHub

While Git is the version control tool, GitHub is a Git server that hosts your Git repositories online, enabling collaboration and project sharing.

  • GitHub: Widely used for public and open-source projects.

  • GitLab: Offers powerful CI/CD capabilities.

  • BitBucket: Popular among corporate users.

Pushing and Pulling in Git

After setting up a remote server, you can push and pull code with:

  • Push: Upload local commits to a Git server.

  • Pull: Fetch and merge code from the server.

In Part 2, we’ll dive into branching, merging, stashing, and more advanced Git workflows to take your Git knowledge to the next level.