Master Git & GitHub

Learn version control and collaboration skills that will serve you throughout your entire career

Get Started

Why Learn Git & GitHub?

📝

Track Changes

Keep a complete history of your code, with the ability to revert to any previous version at any time.

👥

Collaborate

Work seamlessly with others on the same project without overwriting each other's changes.

🚀

Build Your Portfolio

Showcase your work to potential employers and contribute to open-source projects.

Git Basics

Getting Started with Git

Git is a distributed version control system that helps you track changes to files and coordinate work among multiple people.

1. Installing Git

First, you need to install Git on your computer:

2. Configuring Git

After installation, set up your identity:

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

Creating Your First Repository

A repository (or "repo") is a project that Git tracks. Let's create one:

# Create a new directory mkdir my-first-project cd my-first-project # Initialize a Git repository git init
Tip: The git init command creates a hidden .git directory that stores all the version history for your project.

Understanding the Git Workflow

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

  1. Working Directory: Where you modify files
  2. Staging Area (Index): Where you mark files ready to be saved
  3. Repository: Where Git permanently stores changes as commits
Git Workflow Diagram

Basic Git Commands

Tracking Changes

# Check which files have been modified git status # Add files to the staging area git add filename.txt # Add a specific file git add . # Add all files # Commit changes to the repository git commit -m "Add a descriptive message about your changes"

Viewing History

# View commit history git log # View commit history with a graph git log --graph --oneline --all

GitHub Essentials

What is GitHub?

GitHub is a web-based hosting service for Git repositories. It adds many collaboration features on top of Git, including:

Creating a GitHub Account

Visit github.com and sign up for a free account. Students can get additional benefits through the GitHub Student Developer Pack.

Connecting to GitHub

To connect your local Git repository to GitHub:

1. Create a new repository on GitHub

Go to GitHub, click the "+" icon in the top right, and select "New repository".

2. Connect your local repository

# Add the GitHub repository as a remote git remote add origin https://github.com/yourusername/your-repo-name.git # Push your code to GitHub git push -u origin main
Note: GitHub now uses "main" as the default branch name instead of "master". If you're using an older version of Git, you might need to use git push -u origin master instead.

GitHub Workflow

Once your repository is on GitHub, follow this workflow to make changes:

# Pull the latest changes from GitHub git pull origin main # Make your changes locally # ... # Add and commit your changes git add . git commit -m "Describe your changes" # Push your changes to GitHub git push origin main

Collaborating with Others

Branching Strategy

Branches allow you to develop features isolated from each other. Here's a typical workflow:

# Create a new feature branch git checkout -b feature-name # Make changes, add, and commit # Push the branch to GitHub git push origin feature-name

Pull Requests

Pull requests are GitHub's way to propose changes to a repository:

  1. Push your branch to GitHub
  2. Go to the repository on GitHub
  3. Click "Compare & pull request"
  4. Add a title and description
  5. Click "Create pull request"
Pull Request Example

Code Reviews

Code reviews help maintain code quality and share knowledge:

Handling Merge Conflicts

Merge conflicts occur when Git can't automatically merge changes:

# When you get a merge conflict, git status will show you the conflicting files git status # Open the files and resolve the conflicts manually # Look for conflict markers: <<<<<<< HEAD, =======, and >>>>>>> # After resolving, add the files git add . # Complete the merge git commit -m "Resolve merge conflicts"
Tip: Many code editors like VS Code have built-in tools to help resolve merge conflicts.

Advanced Git & GitHub Techniques

GitHub Actions (CI/CD)

GitHub Actions allows you to automate workflows like testing and deployment directly from your repository.

GitHub Pages

Host websites directly from your GitHub repository. Great for portfolios, documentation, and project websites.

Useful Git Commands

Undoing Changes

# Undo uncommitted changes to a file git checkout -- filename.txt # Undo staged changes git reset HEAD filename.txt # Undo a commit (creates a new commit that reverses changes) git revert commit-hash # Amend the previous commit git commit --amend -m "New commit message"

Stashing Changes

# Save changes temporarily without committing git stash # Apply stashed changes git stash pop

Tagging Releases

# Create a tag git tag v1.0.0 # Push tags to GitHub git push origin --tags

Additional Resources

Git Documentation

The official Git documentation is comprehensive and well-written.

Visit

GitHub Learning Lab

Interactive courses to learn Git and GitHub through practical exercises.

Visit

Oh Sh*t, Git!?!

Common Git mistakes and how to fix them. (Warning: contains profanity)

Visit
Reading Progress