← Back to Home

Module 2: Git Basics

Module Overview

In this module, you'll learn about version control using Git. You'll learn how to use Git to move files from a working directory to the Git staging area and a local Git commit, and how to push local changes to make them permanent.

Learning Objectives

Content

Git Basics - Part 1

Understanding Git

Git is a distributed version control system that helps you track changes in your code and collaborate with others. It was created by Linus Torvalds in 2005 for development of the Linux kernel.

Key Concepts in Git

  • Repository (Repo): A storage location for your project, containing all the files and the history of changes made to those files.
  • Working Directory: The directory on your local machine where you modify files.
  • Staging Area (Index): An intermediate area where changes are added before committing.
  • Commit: A snapshot of the changes you've made, saved to the repository history.
  • Branch: A parallel version of the repository that allows you to work on features separately.
  • Remote: A version of your repository hosted on a server (like GitHub).

Basic Git Workflow

  1. Make changes in your working directory
  2. Stage the changes you want to commit
  3. Commit the changes to your local repository
  4. Push the changes to a remote repository (if applicable)

Git Basics - Part 2

Essential Git Commands

Setting Up

  • git init - Initializes a new Git repository in the current directory
  • git clone <url> - Creates a copy of a remote repository on your local machine
  • git config --global user.name "Your Name" - Sets your username for commits
  • git config --global user.email "your.email@example.com" - Sets your email for commits

Working with GitHub Classroom

When working with projects from GitHub Classroom:

  1. Open the GitHub Classroom Repo, click the green Code button, then copy the HTTPS URL
  2. Clone the repository to your local machine: git clone <copied-url>
  3. Create a branch and work only in that branch:
    git branch <name-of-branch>
    git checkout <name-of-branch>

Basic Git Commands

  • Fork - Creates a copy of a repository on your account (typically done through GitHub's web interface)
  • Clone - git clone <url> - Downloads a repository to your local machine
  • Add - git add . - Stages all changes for commit (the . includes all changes)
  • Commit - git commit -m "message" - Creates a snapshot of your staged changes with a descriptive message
  • Push - git push - Uploads your commits to the remote repository
  • Pull Request - A way to propose changes to a repository (done through GitHub's web interface)

Example Git Workflow

# Clone a repository
git clone https://github.com/username/repository.git

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

# Make changes to files...

# Stage and commit changes
git add .
git commit -m "Add new feature"

# Push changes to remote repository
git push --set-upstream origin feature-branch

# Create a pull request on GitHub

Setting Your Upstream

When you're on a new branch, the first time you push you may need to set your upstream:

git push --set-upstream origin <name-of-branch>

After setting the upstream once, you can simply use git push for future pushes.

Curated Content

What is Version Control?

What is Git?

Git - Core Concepts

Guided Project

Working with Branches

In this guided project, you'll learn how to work with Git branches, which are essential for collaborating on projects and developing features in isolation.

Key Skills You'll Practice

  • Creating and switching between branches
  • Managing changes across different branches
  • Resolving conflicts that might arise when merging branches
  • Using branches effectively in a development workflow

Working with Sprint Projects

For sprint projects, it's recommended to:

  1. Branch off of main into a new branch (e.g., sprint-1)
  2. Complete all mastery tasks for that sprint in the branch
  3. When starting a new sprint, create a new branch (e.g., sprint-2) from either the previous sprint branch or main
  4. Make sure each sprint has its own branches on GitHub, as each branch will be independently scored