Welcome to the Git and GitHub for Beginners course!

This comprehensive course will guide you through the fundamentals of version control using Git and how to collaborate effectively on projects using GitHub. Whether you're a student, hobbyist, or professional developer, these essential skills will help you manage code and work with teams efficiently.

Course Outline

1Introduction to Version Control

Learn why version control is essential and how it solves common development challenges.

2Getting Started with Git

Set up Git on your machine and learn the basic commands for everyday use.

3Working with Repositories

Create, clone, and manage repositories to store and track your projects.

4Branching and Merging

Learn how to work on features in isolation and combine code changes effectively.

5Collaboration with GitHub

Master pull requests, issues, and other GitHub features for team collaboration.

6Best Practices

Discover industry-standard workflows and conventions for efficient Git usage.

1. 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 you to revert files to a previous state, compare changes over time, see who made what changes, and more.

Why Version Control Matters

Have you ever worked on a document and saved multiple versions like "document_v1", "document_v2", "document_final", "document_FINAL_FINAL"? Version control systems solve this problem elegantly by tracking all changes in a structured way.

Types of Version Control Systems

  • Local Version Control Systems: Simple database on your local machine
  • Centralized Version Control Systems (CVCS): A single server storing all versioned files (SVN, Perforce)
  • Distributed Version Control Systems (DVCS): Multiple copies of the repository exist (Git, Mercurial)

Benefits of Git

Git is a distributed version control system created by Linus Torvalds in 2005. It offers several advantages:

  • Speed: Git operations are lightning fast
  • Distributed development: Everyone has a full copy of the repository
  • Branching model: Git's branching is lightweight and powerful
  • Data integrity: Git's design ensures the authenticity of code history
  • Staging area: Allows for carefully crafted commits

How Git Works

Git thinks of data as a series of snapshots of a miniature filesystem. When you commit, Git takes a snapshot of all your files and stores a reference to that snapshot.

The Three States

Git has three main states that your files can reside in:

  • Modified: You've changed the file but haven't committed it yet
  • Staged: You've marked a modified file to go into your next commit
  • Committed: The data is safely stored in your local database

Git's Three States Workflow

Files in Git flow through three main states before being saved in the repository:

  1. Working Directory (Modified): Where you make changes to your files
  2. Staging Area (Staged): Where you prepare files for the next commit using git add
  3. Git Repository (Committed): Where your changes are permanently stored using git commit

Self-Check Quiz

  1. What does DVCS stand for?
  2. Who created Git and in what year?
  3. Name the three states that Git files can be in.

2. Getting Started with Git

Installing Git

Before you can start using Git, you need to install it on your computer:

On Windows:

Download and install Git from git-scm.com

On macOS:

# Using Homebrew
$ brew install git

# Or download the installer from git-scm.com
# Visit: https://git-scm.com/download/mac

On Linux:

# Debian/Ubuntu
$ sudo apt-get install git

# Fedora
$ sudo dnf install git

Configuring Git

After installing Git, you should set your username and email address. This is important because every Git commit uses this information:

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"
Pro Tip

You can check your current configuration with:

git config --list

Basic Git Commands

Command Description
git init Initialize a new Git repository
git clone [url] Clone (download) a repository from an existing URL
git status Show the status of the working directory
git add [file] Add a file to the staging area
git commit -m "[message]" Commit changes with a message describing the changes
git log View commit history

Your First Git Project

Let's create a simple project to demonstrate Git basics:

# Create a new directory for your project
$ mkdir my-first-repo
$ cd my-first-repo

# Initialize a new Git repository
$ git init

# Create a simple README file
$ echo "# My First Git Repository" > README.md

# Check the status of your repository
$ git status

# Add the README file to the staging area
$ git add README.md

# Commit the staged changes
$ git commit -m "Initial commit: Add README"

# View the commit history
$ git log

Practice Exercise

  1. Create a new repository for a personal project
  2. Add at least three files with different content
  3. Stage and commit your changes with meaningful commit messages
  4. View your commit history

3. Working with Repositories

Creating and Cloning Repositories

There are two ways to get a Git repository:

  1. Initialize a new repository with git init
  2. Clone an existing repository with git clone

Initializing a New Repository

$ mkdir project-name
$ cd project-name
$ git init

Cloning an Existing Repository

$ git clone https://github.com/username/repository-name.git
$ git clone https://github.com/username/repository-name.git my-folder-name
Note

When you clone a repository, Git automatically sets up a remote called "origin" pointing to the URL you cloned from.

The Git Workflow

The basic Git workflow goes like this:

  1. Modify files in your working directory
  2. Stage the files, adding snapshots to your staging area
  3. Commit the changes, which stores the snapshots permanently in your Git repository

Tracking New Files

# Add a specific file
$ git add filename.txt

# Add all files
$ git add .

# Add all files matching a pattern
$ git add *.html

Commit Your Changes

# Simple commit with message
$ git commit -m "Add new feature: user authentication"

# Commit and skip staging (only for tracked files)
$ git commit -a -m "Update documentation"
Writing Good Commit Messages

A good commit message should:

  • Be concise (50 characters or less for the summary)
  • Use the imperative mood ("Add feature" not "Added feature")
  • Describe what was changed and why

Viewing Changes

# Show changes between working directory and staging area
$ git diff

# Show changes between staging area and last commit
$ git diff --staged

# Show changes in specific file
$ git diff path/to/file

Ignoring Files

You can create a .gitignore file to specify files that Git should ignore:

# Example .gitignore file
# Ignore build output
/build/

# Ignore node_modules
node_modules/

# Ignore log files
*.log

# Ignore IDE specific files
.vscode/
.idea/

Common Git Commands

Here are some additional commands you'll use frequently:

Command Description
git pull Fetch and merge changes from the remote repository
git push Upload local commits to the remote repository
git fetch Download objects and refs from remote repository
git reset [file] Unstage changes (remove from staging area)
git checkout -- [file] Discard changes in working directory
git rm [file] Remove file from working directory and staging area

Challenge Exercise

  1. Create a simple web project with HTML, CSS, and JS files
  2. Create an appropriate .gitignore file
  3. Track your files and make several commits showing your progress
  4. Use git diff to see what changes you've made before committing

4. Branching and Merging

Understanding Git Branches

Branching means diverging from the main line of development and continuing work without messing with the main line. Git's branching model is its "killer feature" because it's incredibly lightweight and switching branches is fast.

What is a Branch?

A branch in Git is simply a lightweight movable pointer to a commit. The default branch name in Git is main (formerly master).

Git Branching Visualization

Imagine your project as a tree with branches. The main branch (trunk) contains your stable code, while feature branches (smaller branches) allow you to work on new features without affecting the main codebase. When a feature is complete, you merge the branch back into main.

Basic Branching Commands

Command Description
git branch List all branches (asterisk marks current branch)
git branch [branch-name] Create a new branch
git checkout [branch-name] Switch to a branch
git checkout -b [branch-name] Create and switch to a new branch
git branch -d [branch-name] Delete a branch

Working with Branches

# Create and switch to a new feature branch
$ git checkout -b feature-login

# Make changes and commit them
$ echo "Login functionality" > login.js
$ git add login.js
$ git commit -m "Add login feature"

# Switch back to main branch
$ git checkout main
# Merge the feature branch into main
$ git merge feature-login
# Delete the feature branch
$ git branch -d feature-login

Resolving Merge Conflicts

Sometimes, when merging branches, you may encounter conflicts. Git will mark the files with conflicts, and you'll need to resolve them manually:

# After a merge conflict
$ git status
# Open the conflicted file and resolve the conflicts    
$ git add resolved-file.txt
$ git commit -m "Resolve merge conflict"
Warning

Always review the changes before committing after resolving conflicts to ensure everything is correct.

Branching Strategies

There are several branching strategies you can adopt, such as:

  • Feature Branching: Create a new branch for each feature or bug fix
  • Git Flow: A more structured approach with multiple branches for features, releases, and hotfixes
  • GitHub Flow: A simplified workflow suitable for continuous deployment

Branching Exercise

  1. Create a new branch for a feature in your project
  2. Make changes and commit them to the feature branch
  3. Merge the feature branch back into the main branch
  4. Resolve any merge conflicts that arise

5. Collaboration with GitHub

What is GitHub?

GitHub is a web-based platform that uses Git for version control. It provides a user-friendly interface for managing Git repositories and offers additional features like issue tracking, pull requests, and project management tools.

Creating a GitHub Account

If you don't have a GitHub account yet, go to github.com and sign up for free.

Setting Up Authentication

To interact with GitHub from your local machine, you'll need to set up authentication. GitHub recommends using Personal Access Tokens or SSH keys:

Using Personal Access Tokens (Recommended)

  1. Go to GitHub Settings → Developer settings → Personal access tokens
  2. Generate a new token with appropriate permissions (repo, workflow)
  3. Use the token as your password when prompted

Using SSH Keys

# Generate SSH key
$ ssh-keygen -t ed25519 -C "your.email@example.com"

# Add SSH key to ssh-agent
$ ssh-add ~/.ssh/id_ed25519

# Copy public key to GitHub (Settings → SSH and GPG keys)

Creating a New Repository on GitHub

  1. Log in to your GitHub account.
  2. Click on the "+" icon in the top right corner and select "New repository".
  3. Name your repository and add a description.
  4. Select "Public" or "Private" based on your preference.
  5. Click "Create repository".

Connecting Your Local Repository to GitHub

To push your local repository to GitHub, you need to add a remote:

# Add a remote repository
$ git remote add origin https://github.com/username/repository-name.git
# Push your local repository to GitHub
$ git push -u origin main

Working with Pull Requests

Pull requests are a way to propose changes to a repository. They allow you to review and discuss changes before merging them into the main branch.

Creating a Pull Request

  1. Push your feature branch to GitHub.
  2. Go to the repository on GitHub.
  3. Click on "Pull requests" and then "New pull request".
  4. Select your feature branch and click "Create pull request".

Reviewing a Pull Request

You can review the changes, add comments, and approve or request changes before merging.

GitHub Collaboration Exercise

  1. Create a new repository on GitHub.
  2. Clone the repository to your local machine.
  3. Create a new branch, make changes, and push it to GitHub.
  4. Create a pull request for your changes.

6. Best Practices

Best Practices for Using Git

  • Commit Often: Make small, frequent commits with clear messages.
  • Use Branches: Use branches for new features or bug fixes.
  • Write Meaningful Commit Messages: Describe what you changed and why.
  • Avoid Committing Large Files: Use .gitignore for files that shouldn't be tracked.
  • Keep Your Repository Clean: Regularly delete old branches and unused files.

Resources for Further Learning

Here are some resources to help you continue learning about Git and GitHub:

Conclusion

Congratulations! You've completed the Git and GitHub for Beginners course. You now have a solid understanding of version control and how to collaborate on projects using Git and GitHub. Keep practicing and exploring more advanced features as you continue your development journey.