Day 12 - Git & GitHub

Version Control System Setup and Basics

What is Git?

Git is a version control system that helps developers track changes in their code over time. It allows multiple people to work on the same project without overwriting each other's work.

Think of Git as a "time machine" for your code - you can go back to any previous version whenever you want!

What is GitHub?

GitHub is a platform that hosts Git repositories online. It's like "Google Drive for code" where you can:

Git Installation

1 Download Git

Visit git-scm.com/downloads and download Git for your operating system.

2 Install Git

Run the installer and follow the setup wizard. Use the default options for most settings.

3 Verify Installation

Open your terminal/command prompt and type:

git --version

You should see the Git version number if installation was successful.

Git Configuration

After installing Git, you need to set up your identity:

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

Replace "Your Name" and "your.email@example.com" with your actual name and email address.

To check your configuration:

git config --list

Basic Git Commands

1 Initialize a Repository

Navigate to your project folder and run:

git init

This creates a new Git repository in your folder.

2 Check Status

See which files have changed:

git status

3 Add Files to Staging

Stage files for the next commit:

git add filename.txt    # Add specific file
git add .              # Add all files

4 Commit Changes

Save your changes with a message:

git commit -m "Your commit message"

5 View History

See your commit history:

git log

GitHub Account Setup

1 Create GitHub Account

Visit github.com and sign up for a free account.

2 Create a Repository

Click the "+" icon in the top right and select "New repository". Give it a name and description.

3 Connect Local Repository to GitHub

After creating a repository on GitHub, you'll see commands to connect your local repository:

git remote add origin https://github.com/yourusername/your-repo-name.git
git branch -M main
git push -u origin main

Typical Git Workflow

# 1. Create or navigate to your project folder
cd my-project

# 2. Initialize Git repository
git init

# 3. Create a new file
echo "# My Project" > README.md

# 4. Check status
git status

# 5. Add files to staging
git add .

# 6. Commit changes
git commit -m "Initial commit"

# 7. Connect to GitHub (only once)
git remote add origin https://github.com/yourusername/my-project.git

# 8. Push to GitHub
git push -u origin main

Common Issues & Solutions

Issue Solution
"git is not recognized" Git is not installed or not in PATH. Reinstall Git.
Authentication failed Use personal access token instead of password for GitHub.
Permission denied Check if you have write access to the repository.
Merge conflicts Manually resolve conflicts in the files, then commit.