Lecture 01-04
1. Introduction to Version Control and Git
Version Control:
o Tracks changes made to files over time.
o Helps in collaboration and prevents conflicts when multiple people work
on the same project.
Git:
o A distributed version control system to manage changes.
o Allows you to save, track, and roll back changes.
2. History of Git
Why Was Git Created?
o Developers needed a reliable way to manage and collaborate on large-
scale projects like the Linux kernel.
Key Milestones:
o 2005: Git created by Linus Torvalds.
o 2007: GitHub launched.
o 2010s: Git became the most popular tool for version control.
3. Git vs GitHub
Feature Git GitHub
Purpose Tool for tracking code Platform for hosting Git repositories
changes. online.
Works Yes. No, requires the internet.
Offline?
Example Use Save changes locally. Share work with a team.
Analogy:
Git is like your personal notebook, while GitHub is like a library where your
notebook is stored for everyone to view and collaborate.
4. Installing Git CLI and GUI
1. Git CLI (Command Line Interface):
o Download Git CLI from [Link].
Configure your name and email after installation:
bash
git config --global [Link] "Your Name"
git config --global [Link] "[Link]@[Link]"
2. GitHub Desktop (GUI):
o Download from [Link].
o Install and log in with your GitHub account.
5. Project: Creating a Personal Notes Repository
Using Git Bash:
Initialize a Repository:
bash
mkdir Notes
cd Notes
git init
1. Add a File:
bash
echo "My first note" > [Link]
git add [Link]
git commit -m "Added first note"
2. Push to GitHub:
bash
git remote add origin <repository-url>
git push -u origin main
3. Using GitHub Desktop:
1. Open GitHub Desktop and click File > New Repository.
2. Fill in the repository name and local path.
3. Add a file (drag and drop into the app).
4. Commit the file and click Publish Repository to push it online.
Lecture 05-08
1. Comparing GitHub and GitLab
GitHub:
o Focus on open-source and community projects.
o Great for beginners and smaller teams.
o Free public repositories.
GitLab:
o Ideal for enterprises and private projects.
o Offers built-in CI/CD for automation.
o Self-hosting options available.
2. Adding Collaborators
Using GitHub:
1. Go to your repository on GitHub.
2. Click Settings > Collaborators and Teams.
3. Enter the collaborator's GitHub username or email.
4. Assign permissions and click Add Collaborator.
Using GitLab:
1. Open your repository on GitLab.
2. Click Settings > Members.
3. Enter the collaborator's username or email.
4. Choose a role (e.g., Developer, Maintainer) and click Add Member.
3. Adding, Updating, and Committing Files
Using Git Bash:
Add Files:
bash
echo "My new file" > [Link]
git add [Link]
1. Update Files:
Make changes to [Link] and stage it again:
bash
git add [Link]
2. Commit Changes:
bash
Copy code
git commit -m "Updated [Link]"
Using GitHub Desktop:
1. Open the repository in GitHub Desktop.
2. Drag and drop new files into the application.
3. Write a commit message and click Commit to Main.
4. Click Push Origin to sync with the remote repository.
4. git status vs git log
Using Git Bash:
git status:
o Shows the current state of the working directory.
bash
git status
git log:
o Displays a history of commits.
git log
Using GitHub Desktop:
View commit history by clicking the History tab.
View file changes under the Changes tab.
5. Project Work: Collaborative Team Task Manager
Goal:
Create a task manager project collaboratively.
Steps in Git Bash:
Initialize a Repository:
bash
mkdir TaskManager
cd TaskManager
git init
1. Add Collaborators:
o Use GitHub or GitLab steps (above).
2. Collaborate:
Each member works on a file:
bash
echo "Task List" > [Link]
git add [Link]
git commit -m "Added task list"
git push
3. Track Changes:
o Use git status to see uncommitted changes.
o Use git log to see the commit history.
Steps in GitHub Desktop:
1. Create a repository and invite collaborators.
2. Each collaborator clones the repository.
3. Files can be added, committed, and synced using the GUI.
Flowchart: Git Workflow
1. Create or Clone Repository → 2. Add Files → 3. Commit Changes → 4. Push to
Remote → 5. Collaborate
Lecture 09-12
Git Basics (Merge, Push, and Pull)
1. Working with Branches and HEAD
What Are Branches?
o Branches allow you to work on different features or updates independently
without affecting the main codebase.
What is HEAD?
o HEAD is a pointer indicating the branch you are currently working on.
Steps to Work with Branches:
o In Git Bash:
bash
# Create a new branch
git branch feature-branch
# Switch to the branch
git checkout feature-branch
o In GitHub Desktop:
1. Click 'Current Branch' > 'New Branch'.
2. Enter the branch name and click 'Create Branch'.
2. Merging Branches
What is Merging?
o Merging combines changes from one branch into another, integrating updates
into a single branch.
Steps to Merge:
o In Git Bash:
bash
# Switch to the branch you want to merge into
git checkout main
# Merge another branch into it
git merge feature-branch
o In GitHub Desktop:
1. Switch to the target branch (e.g., main).
2. Click 'Branch > Merge Into Current Branch' and select the branch
to merge.
3. Push Command
What is Push?
o The push command sends your local commits to the remote repository.
Steps to Push Changes:
o In Git Bash:
bash
# Push changes to the remote repository
git push origin main
# First-time push (sets upstream branch)
git push -u origin main
o In GitHub Desktop:
1. Commit your changes.
2. Click 'Push Origin' to sync with the remote repository.
4. Pull Command
What is Pull?
o The pull command fetches and merges changes from the remote repository
into your local branch.
Steps to Pull Changes:
o In Git Bash:
bash
git pull origin main
o In GitHub Desktop:
1. Click 'Pull Origin' to fetch and merge changes.
5. Resolving Merge Conflicts
What Are Merge Conflicts?
o Conflicts occur when two branches make conflicting changes to the same file,
and Git cannot automatically resolve them.
Steps to Resolve Conflicts:
o In Git Bash:
bash
# Attempt to merge
git merge feature-branch
# Edit conflicting files to resolve issues
# Mark resolved files as staged
git add <file>
# Commit the resolved merge
git commit -m "Resolved merge conflict"
o In GitHub Desktop:
1. Open the 'Changes' tab.
2. Resolve conflicts visually.
3. Commit the resolved changes.
6. Lab Activity: Resolve Merge Conflicts
Problem Statement:
Two team members modify the same file ([Link]) on separate branches. Merge the
branches and resolve the resulting conflict.
Steps:
1. Initialize Repository:
bash
mkdir ConflictActivity
cd ConflictActivity
git init
echo "Project: Collaborative Tool" > [Link]
git add [Link]
git commit -m "Initial commit"
2. Create Branches:
o Member A (feature-A):
bash
git checkout -b feature-A
echo "Feature A: Login functionality" >> [Link]
git add [Link]
git commit -m "Added login feature"
o Member B (feature-B):
bash
git checkout main
git checkout -b feature-B
echo "Feature B: Registration functionality" >> [Link]
git add [Link]
git commit -m "Added registration feature"
3. Merge and Resolve Conflicts:
o Merge branches into main:
bash
git checkout main
git merge feature-A
git merge feature-B
o Resolve conflicts in [Link]:
markdown
<<<<<<< HEAD
Feature A: Login functionality
=======
Feature B: Registration functionality
>>>>>>> feature-B
Update to:
less
Feature A: Login functionality
Feature B: Registration functionality
4. Push Final Changes:
bash
git add [Link]
git commit -m "Resolved merge conflict"
git push origin main
7. Lab Activity Solution
Final [Link]:
Project: Collaborative Tool
Feature A: Login functionality
Feature B: Registration functionality
Key Commands:
o Initialize: git init
o Create Branch: git checkout -b <branch>
o Add Changes: git add <file>
o Commit Changes: git commit -m "message"
o Merge: git merge <branch>
o Conflict Resolution: Edit file, mark as resolved, and use git add.
Summary
1. Branches allow parallel development.
2. Merging integrates changes from one branch into another.
3. Push and Pull synchronize local and remote repositories.
4. Merge Conflicts require manual resolution to finalize changes.
Lecture Notes ( 13-16)
Interaction with Remote Repositories
1. Understanding Remote Repositories
What Are Remote Repositories?
Remote repositories are Git repositories hosted on platforms like GitHub,
GitLab, or Bitbucket.
They allow multiple team members to collaborate by sharing code online.
Examples:
A team project hosted on GitHub.
A private repository for internal development on GitLab.
Key Commands:
Add a remote repository:
bash
git remote add origin <repository-url>
View configured remotes:
bash
git remote -v
2. Cloning and Forking Repositories
Cloning:
Creates a local copy of a remote repository for you to work on.
Command:
bash
git clone <repository-url>
Example:
bash
git clone [Link]
Forking:
Creates a personal copy of a repository on your GitHub account.
Forking is commonly used to propose changes to public repositories or
contribute to open-source projects.
Steps:
1. Open the repository on GitHub.
2. Click the Fork button at the top-right corner.
3. The forked repository will appear in your GitHub account.
3. Code Comparison
Why Compare Code?
Comparing code helps you identify differences between commits, branches, or
files.
Useful for debugging, reviewing changes, or resolving issues.
Key Commands:
Compare working directory with the staging area:
bash
git diff
Compare two branches:
bash
git diff branch1..branch2
Example:
To compare changes in the feature branch with the main branch:
bash
git diff main..feature
4. Push, Pull, and Pull Requests
Push:
Sends local commits to the remote repository.
Command:
bash
git push origin <branch>
Example:
bash
git push origin main
Pull:
Fetches changes from the remote repository and merges them into your local
branch.
Command:
bash
git pull origin <branch>
Example:
bash
git pull origin main
Pull Requests (PR):
A pull request is used to propose changes from a forked repository to the
original repository.
Steps to Create a Pull Request:
1. Push your changes to the forked repository.
2. Go to the original repository on GitHub.
3. Click New Pull Request.
4. Add a description of your changes and submit the PR.
5. Activity: Collaborative Repository
Objective: Practice forking, cloning, making changes, and creating a pull request.
Steps:
1. Fork the Repository:
Fork an open-source repository from GitHub into your GitHub account.
2. Clone the Repository:
Clone the forked repository to your local machine:
bash
git clone <your-forked-repository-url>
1. Make Changes:
Add a new file or modify an existing one:
bash
echo "Hello World" > [Link]
git add [Link]
git commit -m "Added a new file"
1. Push Changes:
Push the changes to your forked repository:
bash
git push origin main
1. Create a Pull Request:
Go to the original repository on GitHub.
Click New Pull Request and describe your changes.
6. Solution: Collaborative Repository
Steps:
1. Fork and Clone:
Fork a repository from GitHub and clone it to your local machine:
bash
git clone <your-forked-repository-url>
1. Make Changes:
Add or update files:
bash
echo "This is a new file" > [Link]
git add [Link]
git commit -m "Added a new file for demonstration"
1. Push and Create a Pull Request:
Push your changes:
bash
git push
Go to the original repository and create a pull request. Add a meaningful description
explaining your changes.
Example Final File ([Link]):
text
This is a new file.
It demonstrates how to add content to a repository and contribute using Git.
7. Summary
1. Remote Repositories:
Enable online collaboration on Git-hosted platforms like GitHub and GitLab.
2. Key Commands:
Clone: git clone <repository-url>
Add Remote: git remote add origin <repository-url>
Push: git push
Pull: git pull
Compare: git diff
3. Pull Requests:
Used to propose and integrate changes into the original repository.
git config : to configure your identity or tools ull be using for working
git config -- list : to view all the configured items
git config --list --global : if above cmd not working