Extra: Getting Started with GitHub

What is Git and GitHub?

Git is a distributed version control system that tracks changes in your code over time. Think of it as a sophisticated "undo" system that remembers every change you've ever made to your files.

GitHub is a cloud-based hosting service for Git repositories. It adds collaboration features like pull requests, issues, and project management tools on top of Git. Think of it as Google Drive for code, but with powerful version control and collaboration features.

Why use Version Control?

  • History Tracking: See who changed what and when

  • Backup: Your code is stored in multiple places

  • Collaboration: Multiple people can work on the same project

  • Branching: Experiment without breaking the main code

  • Rollback: Undo mistakes easily

Part 1: Setting up your GitHub account

If you Create a GitHub account go to github.com click the "Sign up" button and complete registration

Part 2: Installing and Configuring Git

2.1 Install Git on your computer

Windows Installation:

  1. Download Git

    • Click "Download for Windows"

    • The download should start automatically

  2. Run the Installer

    • Double-click the downloaded .exe file

    • Important settings during installation:

      • Default editor: Choose your preferred text editor (VS Code recommended for beginners)

      • PATH environment: Select "Git from the command line and also from 3rd-party software"

      • Line ending conversions: Choose "Checkout Windows-style, commit Unix-style"

      • Terminal emulator: "Use MinTTY"

      • Default branch name: "main" (GitHub's new standard)

      • Keep other settings as default

  3. Verify installation

    • Open Command Prompt or Git Bash

    • Type: git --version

    • You should see something like: git version 2.40.0

Mac Installation:

  1. Check if Git is Pre-installed

    • If you see a version number, Git is already installed

    • If not, continue with installation

  2. Install via Homebrew (Recommended)

  3. Alternative: Install via Xcode

    • Install Xcode Command Line Tools

Linux Installation:

Ubuntu/Debian:

2.2 Configure Git

After installation, you must configure Git with your identity. This information is attached to every commit you make.

Part 3: Authentication Methods

3.1 Understanding authentication options

GitHub offers several authentication methods. As of 2021, password authentication for Git operations was deprecated for security reasons. You now have two main options:

  1. Personal Access Tokens (PAT) - Easier for beginners

  2. SSH Keys - More secure and convenient long-term

3.2 Method 1: Personal Access Token (PAT) authentication

Creating a Personal Access Token:

  1. Navigate to token settings

    • Log into GitHub

    • Click your profile picture → Settings

    • Scroll down to "Developer settings" in the left sidebar

    • Click "Personal access tokens" → "Tokens (classic)"

    • Click "Generate new token" → "Generate new token (classic)"

  2. Configure your token

    • Note: Give it a descriptive name (e.g., "Personal Laptop Token")

    • Expiration: Select duration (90 days recommended for security)

    • Select scopes: For basic operations, check:

      • ✅ repo (Full control of private repositories)

      • ✅ workflow (Update GitHub Action workflows)

      • ✅ write:packages (Upload packages to GitHub Package Registry)

      • ✅ delete:packages (Delete packages from GitHub Package Registry)

    • Click "Generate token"

  3. Save your token

    • IMPORTANT: Copy the token immediately!

    • You won't be able to see it again

    • Store it securely (password manager recommended)

  4. Using your token

    When cloning a private repository or pushing changes:

  5. Storing token credentials (Optional)

    To avoid entering your token repeatedly:

    Windows:

    Mac:

    Linux:

SSH keys provide a more secure and convenient way to authenticate. Once set up, you won't need to enter credentials for each operation.

Step 1: Check for existing SSH keys

If you see these files, you may already have SSH keys. You can use existing ones or create new ones.

Step 2: Generate a new SSH key pair

During key generation:

  1. Press Enter to accept default file location

  2. Enter a passphrase (recommended for security)

  3. Confirm the passphrase

  4. Your key pair is generated!

Understanding what was created:

  • Private key: ~/.ssh/id_ed25519 (NEVER share this!)

  • Public key: ~/.ssh/id_ed25519.pub (This goes to GitHub)

Step 3: Add SSH key to SSH agent

The SSH agent manages your keys and passphrases.

Mac/Linux:

Windows (Git Bash):

Step 4: Copy your public key

Mac:

Linux:

Windows (PowerShell):

Windows (Git Bash):

Step 5: Add SSH Key to GitHub

  1. Go to GitHub.com and log in

  2. Click profile picture → Settings

  3. Click "SSH and GPG keys" in sidebar

  4. Click "New SSH key"

  5. Title: Enter descriptive name (e.g., "Personal MacBook Pro")

  6. Key type: Keep as "Authentication Key"

  7. Key: Paste your public key

  8. Click "Add SSH key"

  9. Confirm with your GitHub password if prompted

Step 6: Test your SSH connection

Step 7: Update repository URLs to use SSH

If you've already cloned repositories using HTTPS, update them to use SSH:

Part 4: Basic GitHub workflow

4.1 Creating a new repository

On GitHub (Remote):

  1. Navigate to repository creation

    • Click "+" icon in top right corner

    • Select "New repository"

  2. Configure repository settings

    • Repository name: Use lowercase with hyphens (e.g., my-awesome-project)

    • Description: Brief explanation of your project

    • Visibility:

      • Public: Anyone can see

      • Private: Only you and collaborators can see

    • Initialize repository:

      • ✅ Add a README file (recommended)

      • Add .gitignore: Select template for your language/framework

      • Choose a license: MIT for open source, or leave blank

    • Click "Create repository"

Locally (on your computer):

4.2 Cloning a Repository

Cloning creates a local copy of a remote repository:

4.3 Essential Git commands

Checking repository status

Adding files to staging area

The staging area is where you prepare changes before committing:

Committing changes

Working with Remotes

Pushing changes

Pulling and Fetching changes

4.4 Understanding Git's three states

Files in a Git repository can be in one of three states:

  1. Modified: You've changed the file but haven't staged it

  2. Staged: You've marked the file to go into next commit

  3. Committed: The data is safely stored in your local database

Part 5: Branching and Merging

5.1 Understanding Branches

Branches allow you to develop features isolated from the main codebase. Think of them as parallel universes for your code.

5.2 Merging Branches

Part 6: Team Collaboration Example

6.1 A Real-World Scenario

Let's walk through a typical day for two developers, Alice and Bob, working on an e-commerce website.

Initial Setup

Repository Structure:

Day 1: Alice Works on Shopping Cart

Day 1: Bob Works on User Authentication

Day 2: Creating Pull Requests

Alice's Pull Request:

  1. Alice goes to GitHub repository page

  2. Clicks "Pull requests" → "New pull request"

  3. Selects base: main ← compare: feature/shopping-cart

  4. Adds title: "Add shopping cart functionality"

  5. Adds description:

  6. Assigns Bob as reviewer

  7. Clicks "Create pull request"

Bob Reviews Alice's Code:

Bob's Pull Request:

Bob creates a similar pull request for his authentication feature.

Day 3: Handling Merge Conflicts

Alice's PR gets merged first. Now Bob needs to update his branch:

6.2 Best Practices for Team Collaboration

Branch Naming Conventions

Commit Message Standards

Part 7: Restoring Files and Undoing Changes

7.1 Restoring a Single File

From the Last Commit

From Staging Area

7.2 Restoring the Entire Repository

To a Previous State

Creating a Backup Before Reset

7.3 Recovering Lost Commits

7.4 Viewing File History

Part 8: Advanced Features and Best Practices

8.1 The .gitignore File

Create a .gitignore file to exclude files from version control:

Global .gitignore

8.2 Stashing Changes

Save work in progress without committing:

8.3 Cherry-Picking Commits

Apply specific commits from one branch to another:

8.4 Interactive Rebase

Clean up commit history before merging:

8.5 Git Aliases for Efficiency

Part 9: GitHub-Specific Features

9.1 Issues

Issues track tasks, bugs, and feature requests:

  1. Creating an Issue:

    • Go to repository → "Issues" → "New issue"

    • Add descriptive title

    • Use markdown for formatting

    • Assign labels (bug, enhancement, documentation)

    • Assign to team members

    • Link to project boards

  2. Issue Templates: Create .github/ISSUE_TEMPLATE/ directory with templates:

9.2 Pull Request Templates

Create .github/pull_request_template.md:

9.3 GitHub Actions (CI/CD)

Create .github/workflows/main.yml:

9.4 GitHub Pages

Host static websites directly from repositories:

  1. Enable GitHub Pages:

    • Go to Settings → Pages

    • Select source branch (usually main or gh-pages)

    • Select folder (/root or /docs)

    • Save

  2. Access your site:

    • URL: https://username.github.io/repository-name/

9.5 Project Boards

Organize work with Kanban-style boards:

  1. Create a Project:

    • Go to Projects → New Project

    • Choose template (Basic Kanban, Automated Kanban)

    • Add columns: To Do, In Progress, Review, Done

  2. Automation:

    • Auto-move issues when PR created

    • Auto-close issues when PR merged

    • Auto-archive completed items

Last updated

Was this helpful?