Essential Git and GitHub Crash Course

Oct 4, 2024

Git and GitHub Crash Course Notes

Introduction

  • Importance of Git for developers
    • Essential for tracking changes and collaboration
    • Industry standard for version control
    • Mentioned in most job descriptions

What is Git?

  • Git is a distributed Version Control System
    • Version Control: Tracks and manages code changes over time
    • Distributed: Every developer has a complete copy of the code base on their machine

Why Use Git?

  • Without Git:
    • Projects become chaotic with multiple versions (e.g., my_project_V1, V2, etc.)
    • Difficult to track changes and collaborate
  • With Git:
    • Automatic tracking of changes
    • Multiple developers can work seamlessly
    • Easy navigation through project history

Getting Started with Git

  1. Installation:

    • Install Git (available for Windows, Mac, and Linux)
    • Verify installation: git --version
  2. Configuration:

    • Set up user information:
      • git config --global user.name 'Your Name'
      • git config --global user.email 'your.email@example.com'
  3. Creating a Repository:

    • Initialize a new Git repository: git init
    • Understanding branches (default branch name is usually main)

Tracking Changes

  • Create files and track them:
    • Use git status to check tracked and untracked files
    • Track files with git add [file] or git add . (adds all files)
    • Commit changes: git commit -m 'commit message'

Viewing and Navigating History

  • View commit history: git log
  • Checkout a previous commit: git checkout [commit hash]
  • Understanding the concept of a Detached HEAD state

Working with Remote Repositories (GitHub)

  1. Creating a Remote Repository:
    • Go to GitHub, create a new repository
  2. Linking Local Repo to Remote:
    • Use git remote add origin [repository URL]
  3. Pushing Changes:
    • Push local changes to GitHub: git push -u origin main

Branching and Merging

  • Branches allow for parallel development without affecting the main project
  • Create a new branch: git branch [branch name]
  • Switch branches: git checkout [branch name]
  • Merge branches: git merge [branch name]

Resolving Merge Conflicts

  • Merge conflicts occur when changes from different branches overlap
  • Steps to resolve:
    1. Checkout to the main branch and pull latest changes
    2. Merge your feature branch into main to resolve conflicts
    3. Manually edit files to resolve the conflicts
    4. Stage and commit the resolved files

Git Commands for Undoing Changes

  1. Resetting Changes:
    • git reset can be used to move back in commit history
    • Variants: soft, mixed, and hard resets
  2. Reverting Changes:
    • git revert [commit hash] creates a new commit that undoes changes without losing history

Using Git Stash

  • Temporarily save uncommitted changes to work on something else:
    • git stash saves changes
    • git stash apply brings back saved changes

Using Git through GUIs

  • Advantages of using GUIs (e.g., WebStorm):
    • Visual interface makes it easier to execute commands without memorization
    • Enables quick actions like create branches, commits, and pushes through buttons instead of commands

Conclusion

  • Importance of practicing Git commands to build confidence
  • Download the reference guide for future help
  • Apply these skills in real-world projects or on your resume