Learnwizy Technologies Logo

Learnwizy Technologies

Class 35: Version Control with Git & GitHub

In any software development project, especially when working in a team, managing changes to your codebase is critical. This is where Version Control Systems (VCS) come into play. Today, we'll focus on the most popular distributed VCS: Git, and its most widely used hosting platform: GitHub.


Introduction to Version Control Systems (VCS)


Introduction to Git

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

The Three States of Git:

Git manages your project files through three main states:

Git States

Basic Git Commands (Local Repository)

Let's go through the essential Git commands for local operations.

1. git init: Initialize a new Git repository.

This command creates a new .git subdirectory in your current directory, which contains all the necessary Git repository files.

cd my-project
git init

2. git status: Check the status of your working directory.

Shows which files are untracked, modified, or staged for commit.

git status

3. git add <file> / git add .: Stage changes.

Moves changes from the Working Directory to the Staging Area.

git add index.html         # Stage a specific file
git add css/               # Stage all files in a directory
git add .                  # Stage all changes in the current directory and subdirectories

4. git commit -m "message": Record staged changes.

Takes the staged snapshot and stores it permanently in your local repository. The message should be descriptive.

git commit -m "Add initial HTML structure for homepage"

5. git log: View commit history.

Shows a list of all commits in the current branch, including commit hash, author, date, and commit message.

git log
git log --oneline # Concise view

6. git diff: Show changes.

Compares changes between various Git states.

7. git restore: Unstage files, discard changes.

Used to undo changes.

8. .gitignore: Excluding files from version control.

A plain text file where each line specifies a pattern for files or directories that Git should ignore (e.g., node_modules/, .env files, build artifacts).

# .gitignore example
node_modules/
.env
build/
dist/
*.log

Git Branching and Merging

Branches are fundamental to Git and enable parallel development.


Introduction to GitHub (Remote Repository)

While Git manages your local repository, GitHub is a web-based platform that provides hosting for Git repositories. It's the most popular platform for collaborative development and open-source projects.

1. Creating a new repository on GitHub:

Go to github.com, log in, and click "New repository". Follow the prompts. You'll get a URL (e.g., https://github.com/your-username/your-repo.git).

2. git remote add origin <url>: Connecting local repo to remote.

This command tells your local Git repository where its remote counterpart is located. origin is the conventional name for the main remote repository.

git remote add origin https://github.com/your-username/your-repo.git

3. git push origin <branch_name>: Uploading local commits to remote.

Sends your committed changes from your local branch to the remote repository.

git push origin main        # Push changes from local 'main' to remote 'main'
git push -u origin main     # The first time, use -u to set upstream tracking

4. git pull origin <branch_name>: Downloading and integrating remote changes.

Fetches changes from the remote repository and automatically merges them into your current local branch.

git pull origin main

5. git clone <url>: Copying a remote repository locally.

Downloads an entire repository, including all its history and branches, from a remote server to your local machine.

git clone https://github.com/your-username/your-repo.git

Collaborative Workflow with GitHub

GitHub provides powerful features for team collaboration.


Reverting Changes

Sometimes you need to undo commits. Git offers a few ways:

Git and GitHub are indispensable tools for any modern developer. Mastering them will significantly improve your workflow, collaboration, and code quality. In the next class, we'll begin to explore how to deploy your web applications to the cloud.