Table of Contents
1. Introduction to Git.................................................3
Key Concepts...........................................................................3
Why Use Git?.......................................................................... 3
2. Git Basics: init, add, commit, status................................3
Setting Up Git..........................................................................3
Creating a New Repository..........................................................4
Adding and Committing Files.......................................................4
Lifecycle of a file in Git:.............................................................4
3. Working with Branches............................................4
Common Branches:................................................................... 4
Commands:............................................................................. 4
4. Merging & Resolving Conflicts.....................................5
Merging Branches.....................................................................5
Conflict Example......................................................................5
Resolving Conflict....................................................................5
5. Remote Repositories with GitHub..................................6
Linking Local Repo to GitHub......................................................6
Pulling Changes........................................................................6
6. Pull Requests & Code Reviews.....................................6
Typical Workflow:.................................................................... 6
PRs enable:............................................................................. 6
7. GitHub Issues & Project Management.............................7
Issues.....................................................................................7
Projects (Kanban Boards)............................................................7
1
Example Workflow:...................................................................7
8. Typical Git Workflow Diagram.....................................8
9. Best Practices for Using Git & GitHub.............................8
10. Example Real-World Workflow...................................9
Scenario: Adding a new search feature............................................9
11. Diagram – Git Branching Model..................................9
2
MODULE 25: FOR GIT AND GITHUB
VERSION CONTROL
1. Introduction to Git
Git is a distributed version control system (DVCS) that tracks changes in source code and
allows multiple developers to collaborate efficiently.
Key Concepts:
Version Control: Keeps a history of every change made to files in a project.
Distributed: Every developer’s machine has a full copy of the repository’s history.
Snapshots, not diffs: Git stores snapshots of the project state instead of just file
differences.
Why Use Git?
Tracks changes over time.
Enables collaboration across teams.
Allows branching and merging for parallel development.
Safeguards against accidental data loss.
2. Git Basics: init, add, commit, status
Setting Up Git
# Install Git (if not installed)
sudo apt install git # Linux
brew install git # macOS
# Windows: download installer from [Link]
# Configure Git (first time setup)
git config --global [Link] "Your Name"
git config --global [Link] "youremail@[Link]"
git config --global [Link] auto
3
Creating a New Repository
# Initialize a new repository
git init
# Check repository status
git status
git init creates a hidden .git directory to track changes.
Adding and Committing Files
# Add file(s) to staging area
git add [Link]
git add . # add all files
# Commit staged changes with a message
git commit -m "Initial commit"
# Check commit history
git log
Lifecycle of a file in Git:
1. Untracked → git add → Staged
2. Staged → git commit → Tracked in repo
3. Tracked → Changes → Modified
3. Working with Branches
Branches allow parallel development without interfering with the main codebase.
Common Branches:
main or master: Stable production branch.
feature/*: For adding new features.
bugfix/*: For fixing bugs.
Commands:
# Create a new branch
git branch feature-login
4
# Switch to the new branch
git checkout feature-login
# or
git switch feature-login
# View all branches
git branch
# Create and switch in one step
git checkout -b feature-signup
4. Merging & Resolving Conflicts
Merging Branches
# Switch to main branch
git checkout main
# Merge feature branch into main
git merge feature-login
Conflict Example
If both branches changed the same line in a file, Git flags a conflict:
<<<<<<< HEAD
print("Hello from main branch")
=======
print("Hello from feature branch")
>>>>>>> feature-login
Resolving Conflict
1. Edit the file manually to fix the conflict.
2. Mark conflict as resolved:
git add conflicted_file.py
git commit
5
5. Remote Repositories with GitHub
GitHub is a cloud-based platform for hosting Git repositories, enabling remote
collaboration.
Linking Local Repo to GitHub
1. Create a new repo on GitHub (no README).
2. Link it locally:
git remote add origin [Link]
# Push local branch to GitHub
git push -u origin main
Pulling Changes
# Fetch changes
git fetch origin
# Merge fetched changes into current branch
git pull
6. Pull Requests & Code Reviews
Pull Request (PR): A request to merge changes from one branch (e.g., feature branch) into
another (e.g., main).
Typical Workflow:
1. Create a new branch locally.
2. Push the branch to GitHub.
3. Open a Pull Request on GitHub.
4. Reviewers comment or request changes.
5. Merge PR into the main branch.
PRs enable:
Peer review for code quality.
Safe integration of new features.
6
Automatic CI/CD pipeline triggers.
7. GitHub Issues & Project Management
GitHub provides tools to track bugs, features, and tasks.
Issues
Each issue describes a bug, feature, or task.
Can be labeled (bug, enhancement, urgent), assigned, and linked to PRs.
Projects (Kanban Boards)
Organize issues into To Do → In Progress → Done workflows.
Helps teams manage progress visually.
Example Workflow:
1. Create an issue: “Add login feature.”
2. Assign to a developer and move to “In Progress.”
3. Developer creates feature-login branch, codes the feature, and opens a PR.
4. PR is reviewed, merged, and issue is closed.
7
8. Typical Git Workflow Diagram
9. Best Practices for Using Git & GitHub
Commit often, commit small: Easier debugging and rollbacks.
Write meaningful commit messages (e.g., fix: handle null input in login API).
Use branches for every new feature or bug fix.
8
Keep the main branch always deployable.
Regularly pull and sync to avoid large conflicts.
Protect main branch using branch protection rules.
10. Example Real-World Workflow
Scenario: Adding a new search feature
# Clone repo
git clone [Link]
cd app
# Create feature branch
git checkout -b feature-search
# Work on code
vim [Link]
git add [Link]
git commit -m "feat: add search bar UI"
# Push to GitHub
git push origin feature-search
# Open a Pull Request on GitHub
# After approval, merge PR
11. Diagram – Git Branching Model
main (stable)
│
├─── feature/login
│ └───> merged back into main
│
└─── feature/signup
└───> merged back into main
9
10