Week 0: Git & GitHub
Prerequisite - Version Control & Collaboration
Al-Aqsa University
Week 0: Git & GitHub
Version Control and Team Collaboration - The Foundation for Every Developer
Introductory Week - Used Throughout the Course
1. What is Git? Version Control System
The Problem Without Git
Imagine you are working on a project:
- You made a change and broke the project - how do you revert to the old version?
- You and your teammate are working on the same file - how do you merge your work?
- You need to go back to a version from a week ago - where is it?
- You want to try a new idea without breaking the original code
The Solution: Git
Git is a Version Control System that saves every change you make to a project, allowing you to go back to any previous point.
- Git = A tool on your machine (local) that tracks changes
- GitHub = A website on the internet that stores your projects (cloud) and enables collaboration
Your Machine (Local)
→
Git (Tracks Changes)
→
GitHub (Cloud)
How Does Git Work? - The Three Areas
Git has 3 main areas:
Working Directory
The files you are working on - current changes
Staging Area
Files prepared for saving (git add)
Repository
Changes saved permanently (git commit)
Remote (GitHub)
The online copy (git push)
Edit File
git add →
Staging
git commit →
Local Repo
git push →
GitHub
2. Installation & Setup Installation & Setup
Installing Git
- Windows: Download from git-scm.com and choose default settings
- Mac: Open Terminal and type
git --version (usually pre-installed)
- After installation, open Terminal/Git Bash and verify:
TerminalVerify Installation
git --version
# git version 2.43.0
Initial Setup (One Time Only)
You need to introduce yourself to Git - this information appears with every commit:
TerminalGit Setup
# Set your name
git config --global user.name "Your Name Here"
# Set your email (same as your GitHub email)
git config --global user.email "your@email.com"
# Verify settings
git config --list
Creating a GitHub Account
Go to github.com and click Sign up
Enter your email, username (choose a professional name), and password
Verify your account via email
Complete your profile (photo + Bio)
3. Basic Commands Basic Commands
Creating a New Repository
Terminalgit init
# Navigate to your project folder
cd my-project
# Turn the folder into a Git repository
git init
# Initialized empty Git repository in /my-project/.git/
# Now Git tracks every change in this folder
Note
The command git init creates a hidden folder called .git - this contains the entire project history. Do not delete it!
Checking Project Status - git status
Terminalgit status
git status
# Possible states:
# Untracked files → New files Git doesn't know about
# Modified → Files that have been changed
# Staged → Files ready for commit
# Nothing to commit → Everything is saved
Adding Files to Staging - git add
Terminalgit add
# Add a single file
git add index.html
# Add multiple files
git add index.html style.css script.js
# Add all modified and new files
git add .
# Add all files of a specific type
git add *.html
Saving Changes - git commit
Terminalgit commit
# Save with a descriptive message
git commit -m "add homepage HTML structure"
# Add + save in one command (tracked files only)
git commit -am "fix navigation links"
How to Write a Good Commit Message?
- - Start with a verb:
add, fix, update, remove, refactor
- - Be specific: "fix login button color" not "fix stuff"
- - Write in English (global convention)
- - Keep it short: less than 50 characters
Viewing History - git log
Terminalgit log
# Show all commits
git log
# Show summary (one line per commit)
git log --oneline
# Show last 5 commits
git log --oneline -5
# Show with a graph of branches
git log --oneline --graph --all
Viewing Changes - git diff
Terminalgit diff
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Show changes in a specific file
git diff index.html
Undoing Changes
TerminalUndo Changes
# Discard file changes (before git add)
git checkout -- index.html
# Remove a file from staging (after git add)
git reset HEAD index.html
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Go back to a specific commit (use with caution!)
git checkout abc1234
Warning
Commands that delete changes (like git reset --hard) cannot be undone! Use them with caution.
.gitignore File
A file that tells Git to ignore certain files (not track them):
.gitignoreFiles to Ignore
# System files
.DS_Store
Thumbs.db
# Dependencies folders
node_modules/
vendor/
# Environment files (contain passwords!)
.env
.env.local
# Temporary files
*.log
*.tmp
# Build folder
dist/
build/
4. GitHub The Cloud Platform
What is GitHub?
- A platform to store Git projects on the internet (Cloud)
- Allows collaboration with other developers
- Serves as a Portfolio for your programming work
- Includes tools: Issues, Pull Requests, Actions, Pages
Creating a Repository on GitHub
Go to GitHub and click "+" then "New repository"
Choose a project name (e.g.: my-portfolio)
Choose Public or Private
Select "Add a README file" + "Add .gitignore"
Click "Create repository"
GitHub Interface
- Code: Project files + code viewer
- Issues: Track bugs and tasks
- Pull Requests: Code merge requests
- Actions: Automation (CI/CD)
- Settings: Project settings
- README.md: Project description (first thing visitors see)
5. Working with Remote Connecting GitHub to Your Machine
Method 1: Clone - Copy a Project from GitHub
If the project exists on GitHub and you want to download it to your machine:
Terminalgit clone
# Copy a project from GitHub to your machine
git clone https://github.com/username/project-name.git
# Navigate into the folder
cd project-name
# Now the project is on your machine and linked to GitHub automatically
Method 2: Link a Local Project to GitHub
If you have a project on your machine and want to upload it to GitHub:
TerminalLink Existing Project
# 1. Create an empty repository on GitHub (without README)
# 2. In your project folder on your machine:
git init
git add .
git commit -m "initial commit"
# 3. Link the project to GitHub
git remote add origin https://github.com/username/project.git
# 4. Push the code
git push -u origin main
Pushing and Pulling Changes
Terminalpush & pull
# Push changes from your machine to GitHub
git push
# First time pushing to a new branch
git push -u origin main
# Pull changes from GitHub to your machine
git pull
# Show linked remotes
git remote -v
Your Machine
git push →
GitHub
← git pull
Teammate's Machine
6. Branches Branches
What Are Branches?
Branches allow you to work on a new feature or fix without affecting the original code (main).
main: o---o---o-----------o---o (original code)
\ /
feature: o---o---o---o (new feature)
Branch Commands
TerminalBranches
# List all branches
git branch
# Create a new branch
git branch feature-navbar
# Switch to a branch
git checkout feature-navbar
# Create + switch in one command
git checkout -b feature-navbar
# Or the modern way
git switch -c feature-navbar
# Go back to main
git checkout main
# Merge a branch into main
git checkout main
git merge feature-navbar
# Delete a branch (after merging)
git branch -d feature-navbar
Resolving Merge Conflicts
When you and your teammate edit the same line in the same file, Git cannot merge automatically:
File with ConflictMerge Conflict
<h1>
<<<<<<< HEAD
Welcome to our website <-- Your change
=======
Hello and welcome to the site <-- Teammate's change
>>>>>>> feature-branch
</h1>
Solution: Remove the conflict markers, choose the correct text, then git add . and git commit
7. Daily Workflow Daily Workflow
The Ideal Workflow for Every Project
TerminalDaily Workflow
# === Start of Work ===
# 1. Pull the latest updates
git pull
# 2. Create a branch for the feature you want to work on
git checkout -b feature-contact-form
# 3. Work on the code... edit... add...
# 4. See what changed
git status
git diff
# 5. Add changes
git add .
# 6. Save (commit)
git commit -m "add contact form with validation"
# 7. Push to GitHub
git push -u origin feature-contact-form
# 8. On GitHub: Open a Pull Request to merge the branch into main
# === After Merging ===
git checkout main
git pull
git branch -d feature-contact-form
Pull Request (Merge Request)
Instead of merging directly into main, you create a Pull Request on GitHub:
Push your branch to GitHub with git push
On GitHub: Click "Compare & pull request"
Write a description of the changes you made
Your teammates review the code (Code Review)
After approval: Click "Merge pull request"
README.md - Project Description
Every project should have a README.md file that explains:
README.mdMarkdown
# Project Name
A short description of the project
## Features
- Feature 1
- Feature 2
## Installation
```
git clone https://github.com/user/project.git
cd project
```
## Technologies Used
- HTML5
- CSS3
- JavaScript
## Developer
**Your Name** - [GitHub](https://github.com/username)
8. Commands Reference Commands Reference
| Command |
Description |
Example |
git init |
Create a new repository |
git init |
git clone |
Copy a project from GitHub |
git clone URL |
git status |
Show file status |
git status |
git add |
Add to staging |
git add . |
git commit |
Save changes |
git commit -m "msg" |
git push |
Push to GitHub |
git push origin main |
git pull |
Pull from GitHub |
git pull |
git log |
View history |
git log --oneline |
git diff |
View changes |
git diff |
git branch |
Manage branches |
git branch -a |
git checkout |
Switch to a branch |
git checkout -b name |
git merge |
Merge a branch |
git merge feature |
git remote |
Manage remotes |
git remote -v |
git stash |
Temporarily store changes |
git stash / git stash pop |