0% found this document useful (0 votes)
2 views

Introduction to Version Control With Git

This document provides an introduction to version control with Git, explaining its core concepts such as tracking changes, branches, commits, and merging. It highlights the importance of Git for team collaboration, project management, and code quality, along with essential Git commands and GitHub workflows for creating repositories, cloning, pushing, and pulling code. The document emphasizes the benefits of using branches for feature development and the significance of pull requests for code review.

Uploaded by

Mohana D
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Version Control With Git

This document provides an introduction to version control with Git, explaining its core concepts such as tracking changes, branches, commits, and merging. It highlights the importance of Git for team collaboration, project management, and code quality, along with essential Git commands and GitHub workflows for creating repositories, cloning, pushing, and pulling code. The document emphasizes the benefits of using branches for feature development and the significance of pull requests for code review.

Uploaded by

Mohana D
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to Version Control with Git

Version control is a system that helps track changes in files over time.
It enables multiple people to collaborate on projects, preserves previous
versions of code or documents, and allows teams to merge contributions
efficiently. In the context of software development, version control provides an
organized way to manage changes to source code, keeping track of who
changed what and when.
Core Concepts in Version Control
1. Tracking Changes:
o Version control tools record all changes made to files in a project,
creating a history of edits. This allows developers to revert to
earlier versions, view previous states, and understand the evolution
of the project.
o Each time changes are saved, a commit (a record of modifications)
is created, preserving a snapshot of the project at a specific time.
2. Branches:
o Branching allows multiple copies of the project’s codebase to
coexist independently. A branch is like a parallel version of the
project, where developers can work on features, bug fixes, or
experiments without impacting the main codebase.
o For example, developers often create a new branch for each feature
or bug fix. This isolation allows work on new features without
disturbing the main project.
3. Commits:
o A commit is a save point that records all changes made to the code.
Each commit has a unique identifier (called a commit hash) and
usually includes a brief message explaining what was changed.
o Commits form the history of a project. As each change is recorded,
developers can revisit previous commits to see how the code
looked at any stage.
4. Merging:
o Merging is the process of combining changes from one branch into
another. For instance, once a feature is complete on a feature
branch, it is merged back into the main branch (often called main
or master).
o Merging is essential for incorporating parallel work from team
members into a cohesive codebase. Git includes tools to handle
conflicts, which occur when changes on different branches affect
the same part of a file.
Why Git is Essential for Team Collaboration and Project Management
Git is one of the most widely used version control systems, particularly
popular for software development projects. It is distributed, meaning
every team member has a complete copy of the project history, allowing
for independent work without a constant connection to a central server.
Here’s why Git is crucial for collaborative work:
1. Facilitates Team Collaboration:
o With Git, multiple people can work on the same project
simultaneously, even on the same files. This prevents conflicts and
lost work, as Git handles parallel changes using branches and
merges.
o Developers can pull the latest changes from others, push their work
when ready, and have Git handle integrations smoothly.
2. Enhances Code Quality through Review Processes:
o Git supports pull requests or merge requests workflows, where
developers can request a review of their code before merging it into
the main project. This review process helps maintain code quality,
consistency, and prevents bugs.
3. Improves Project Organization and Version Management:
o Git allows the project to be broken into logical segments using
branches for each new feature, release, or bug fix. This makes it
easy to see which features are in development, test different
versions, or go back to older versions when necessary.
o For instance, by creating a new branch for a major feature,
developers can avoid deploying incomplete features to production,
maintaining stability.
4. Provides Safety and Flexibility:
o Every developer’s copy of the repository is complete with the full
history, making Git a resilient tool that works even offline. Each
commit is like a checkpoint, allowing easy rollback to previous
versions if issues arise.
o This aspect of Git provides safety and flexibility, as developers can
confidently experiment, knowing they can revert changes if
needed.
5. Essential for Open-Source and Distributed Teams:
o Many open-source projects use Git, allowing developers
worldwide to contribute. By creating forks (independent copies)
and pull requests, contributors can suggest changes, and
maintainers can review and merge them if approved.
o Git is also crucial for remote and distributed teams, as it allows
smooth collaboration across time zones and locations without the
risk of losing work.
Basic Git Commands:
▪ git init, git add, git commit – initializing a repository, adding files,
and committing changes.
▪ git status, git log, git branch – viewing the current status, commit
history, and branches.
Git is a powerful version control tool that helps developers track changes,
manage project versions, and collaborate with others. Here’s an in-depth
explanation of some fundamental Git commands:

Initializing a Repository: git init


● Command: git init
● Purpose: Creates a new, empty Git repository in the current directory.
● Usage:
o Open a terminal in the directory where you want to initialize the
Git repository.
o Run git init.
● Explanation:
o This command turns the current directory into a Git repository,
setting up a .git folder where Git will track all changes.
o This is typically the first step in making a project trackable by Git.
Adding Files to the Staging Area: git add
● Command: git add <file_name> or git add .
● Purpose: Moves changes (new or modified files) to the staging area in
preparation for committing.
● Usage:
o To add a specific file: git add <file_name>
o To add all modified and new files: git add .
● Explanation:
o The staging area is a space where changes are listed to be
committed. Adding files to this area allows you to control which
files will be included in the next commit.
o This command can also be used repeatedly to add multiple files
before committing.
Committing Changes: git commit
● Command: git commit -m "commit message"
● Purpose: Creates a commit, which is a snapshot of the changes added to
the staging area.
● Usage:
o Run git commit -m "Description of changes"
● Explanation:
o A commit in Git represents a saved state of the project, allowing
you to roll back to this point in the future if needed.
o The -m flag allows you to include a commit message describing
what changes were made, which is useful for tracking the history
and purpose of each change.
● Example: git commit -m "Add new feature to the project"
Viewing the Current Status: git status
● Command: git status
● Purpose: Shows the current state of the repository, including untracked
files, changes staged for commit, and the current branch.
● Usage:
o Run git status in the terminal within your Git repository.
● Explanation:
o This command is essential for getting an overview of the
repository’s state. It displays files that have been modified but not
yet staged, files in the staging area, and any untracked files.
o Knowing the status of each file helps ensure that all intended
changes are committed and that nothing is accidentally left out.

Viewing the Commit History: git log


● Command: git log
● Purpose: Displays a history of commits in the current branch, showing
details like the commit hash, author, date, and commit message.
● Usage:
o Run git log to view all commits, or use options like git log --
oneline for a simplified view.
● Explanation:
o This command is crucial for tracking the history of a project and
understanding what changes were made, by whom, and when.
o The git log output can also include information on branches,
making it easier to see changes and navigate the commit history.

Working with Branches: git branch


● Command: git branch <branch_name>, git branch
● Purpose:
o git branch <branch_name> creates a new branch.
o Running git branch without arguments lists all branches and shows
the current branch.
● Usage:
o To create a new branch: git branch feature-branch
o To list all branches: git branch
● Explanation:
o Git branches allow parallel workstreams, enabling developers to
isolate new features, bug fixes, or experiments without affecting
the main codebase.
o Branching is an essential part of Git workflow, especially for team
collaboration and version control. Developers can work
independently on branches and then merge changes back into the
main branch when complete.
Summary Table
Com
Purpose Usage Description
mand
Sets up Git tracking
Initialize
in a directory,
git init a git init
creating a new
repository
repository.
Moves changes to
Add files git add
the staging area,
to the <file_name
git add marking them for
staging > or git
inclusion in the next
area add .
commit.
Creates a snapshot
Commit
git git commit of the staged
changes
commi -m changes, with a
with a
t "message" description of the
message
changes made.
git Check git status Shows the state of
status repository files in the repo,
status including staged,
Com
Purpose Usage Description
mand
unstaged, and
untracked files.
Displays a list of
View commits with details
git log commit git log like the author, date,
history and commit
message.
Lists branches,
git branch shows current
git Manage or git branch, or creates a
branch branches branch new branch for
<name> parallel
development.

Why These Commands are Essential for Beginners


● Workflow Foundation: These basic commands cover the essential steps
in a typical Git workflow: initializing a project, adding and committing
changes, checking the status, and viewing project history.
● Collaboration Support: Knowing how to manage branches with git
branch is especially important when working in teams. Branches enable
developers to work on features without interfering with the work of
others, and they help in organizing the development process.
● Error Prevention: Regular use of git status and git log helps track
changes and keep an organized history, making it easier to avoid or
resolve conflicts.
These commands form the foundation for using Git effectively, helping
developers maintain a clear, structured approach to managing code
changes, facilitating collaboration, and building a robust version history
for their projects.
GitHub Workflow:
▪ Creating a GitHub repository, cloning, pushing, and pulling
code.
▪ Importance of creating branches for feature development and
making pull requests for code review.
GitHub Workflow: Creating Repositories, Cloning, Pushing, Pulling,
Branching, and Pull Requests
GitHub is a popular platform for hosting and sharing Git repositories,
supporting collaborative development and version control. Below is an
in-depth explanation of some essential GitHub workflows and
commands.

Creating a GitHub Repository


● Purpose: A GitHub repository is an online space where your project code
is stored and versioned.
● Steps to Create:
1. Log in to your GitHub account.
2. Click on the “New” button to create a new repository.
3. Provide a name for the repository, add a description (optional), and
set the repository to public or private.
4. (Optional) Initialize with a README file and a .gitignore if
needed.
5. Click “Create repository”.
● Explanation: Creating a GitHub repository allows you to store and share
your project with others, enabling collaboration. It serves as a central
place for others to access the latest project code and contribute.

Cloning a GitHub Repository


● Command: git clone <repository_url>
● Purpose: Cloning creates a local copy of a GitHub repository on your
computer, allowing you to work on it offline.
● Steps to Clone:
1. Go to the GitHub repository page and copy the repository’s URL
(usually found under the Code button).
2. Open a terminal on your local machine and navigate to the
directory where you want the repository.
3. Run git clone <repository_url>.
● Explanation: Cloning is essential to work on a project locally. It copies
all files, branches, and commit history from GitHub, enabling offline
development and testing.

Pushing Code to GitHub


● Command: git push origin <branch_name>
● Purpose: Pushing uploads your local commits to the GitHub repository.
● Steps to Push:
1. Ensure your changes are committed locally (using git commit).
2. Use git push origin <branch_name> to upload the branch to
GitHub.
● Explanation: Pushing is essential for sharing your updates with other
collaborators. It syncs your local changes with the remote repository on
GitHub, making them accessible to everyone working on the project.

Pulling Code from GitHub


● Command: git pull origin <branch_name>
● Purpose: Pulling retrieves the latest updates from a GitHub repository
and merges them into your local branch.
● Steps to Pull:
1. Run git pull origin <branch_name> to fetch and integrate the latest
changes from GitHub.
● Explanation: Pulling ensures that your local code is in sync with the
remote repository. It’s essential to pull frequently to avoid conflicts,
especially when working in a team where multiple developers make
updates.

Importance of Creating Branches for Feature Development


Branches allow developers to work on new features, bug fixes, or
experiments in isolation from the main codebase. Here’s why they are
important:
1. Isolate Development:
o A feature branch allows you to work on a specific part of the
project without affecting the main codebase. This prevents
incomplete or experimental features from disrupting the main
functionality.
2. Facilitate Collaboration:
o Multiple developers can create their own branches to work on
different features simultaneously, improving productivity and
minimizing conflicts.
3. Organize Project History:
o Using branches creates a clear, organized history of the project.
Developers can see which features are in progress and which are
complete, improving team communication and tracking.
4. Maintain a Stable Main Branch:
o By creating feature branches, the main branch (often called main or
master) remains stable and is typically reserved for production-
ready code. New features are merged into the main branch only
after they’re thoroughly tested and approved.
Steps to Create and Switch to a New Branch
● Commands:
o Create a branch: git branch <branch_name>
o Switch to the branch: git checkout <branch_name>
● Example:
o git branch feature-xyz (creates a new branch called feature-xyz)
o git checkout feature-xyz (switches to the feature-xyz branch)
Making Pull Requests for Code Review
A pull request (PR) is a way to propose changes from one branch to
another in a GitHub repository. It allows for code review and discussion
before merging code into the main branch.
1. What is a Pull Request?
o A pull request is a request to merge your changes from a feature
branch into another branch (usually the main branch). It enables
team members to review your code, suggest improvements, and
approve changes.
2. Why Pull Requests are Important
o Code Quality: They ensure that code changes are reviewed before
merging, which improves quality and catches bugs early.
o Team Collaboration: Pull requests allow team members to discuss
changes, ask questions, and suggest improvements, fostering
collaboration and shared knowledge.
o Documentation: Pull requests provide a record of changes, making
it easy to track what features were added, bugs fixed, and who
contributed.
3. How to Make a Pull Request
o Step 1: Push your feature branch to GitHub using git push origin
<branch_name>.
o Step 2: Go to the GitHub repository, where you will see an option
to create a pull request for the pushed branch.
o Step 3: Click “Compare & pull request”.
o Step 4: Add a title and description for your pull request, explaining
the changes you made.
o Step 5: Click “Create pull request” to submit it for review.
4. Code Review Process with Pull Requests
o Other team members can view the pull request, add comments, and
suggest changes.
o If changes are requested, the developer can make updates to the
feature branch and push them to GitHub. The pull request will
automatically reflect these changes.
o Once the pull request is approved, it can be merged into the main
branch.

Summary Table of Essential GitHub Commands


Purpo Descript
Command Usage
se ion
Creates a
local
copy of
Clone
the
a
git clone repositor
git clone GitHu
https://github.com/us y,
<repo_url> b
er/repo enabling
reposi
offline
tory
work and
local
testing.
Uploads
local
commits
Push
git push to
chang
origin git push origin GitHub,
es to
<branch_n feature-branch sharing
GitHu
ame> updates
b
with
collabora
tors.
Purpo Descript
Command Usage
se ion
Retrieves
updates
from
Pull
GitHub,
latest
git pull keeping
chang
origin your
es git pull origin main
<branch_n local
from
ame> repositor
GitHu
y in sync
b
with the
latest
code.
Creates a
new
branch to
Create
git branch isolate
a new git branch feature-
<branch_n develop
branc branch
ame> ment for
h
specific
features
or fixes.
Allows
for
review,
discussio
Reque
n, and
st
Pull approval
code On GitHub repository
Request before
revie page
(PR) merging
w and
changes
merge
into the
main
codebase
.
Conclusion
This GitHub workflow is fundamental for team-based projects and open-source
development. By using branches and pull requests, teams maintain high code
quality, support organized development, and enable collaborative workflows.
These practices ensure that all code is reviewed, stable, and follows project
standards, ultimately leading to a more reliable and maintainable codebase.

Hands-On Activity:
1. Basic Git and GitHub Workflow:
▪ Create a local Git repository, add a sample HTML file, make
a commit.
▪ Push the repository to GitHub and verify the upload.
▪ Create a new branch, add a commit, and push the branch to
GitHub, simulating a collaborative workflow.
Step 1: Create a Local Git Repository
1. Open Terminal or Command Prompt.
2. Navigate to the folder where you want to create your project:
bash
cd path/to/your/folder
3. Initialize a Git repository in this folder:
bash
git init
o Explanation: git init initializes a new Git repository in your folder,
creating a .git folder where Git will manage version control.

Step 2: Add a Sample HTML File


1. Create an HTML file in your project folder:
o Open a text editor (such as VS Code) and create a file named
index.html.
o Add the following HTML code to index.html:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Git & GitHub Workflow</title>
</head>
<body>
<h1>Hello, Git!</h1>
</body>
</html>
2. Save the file.

Step 3: Stage and Commit the File


1. Stage the HTML file:
bash

git add index.html


o Explanation: git add stages your changes, preparing them to be
committed. This tells Git that you want to track this file.
2. Commit the file:
bash
git commit -m "Add index.html with sample HTML code"
o Explanation: git commit saves the current changes to the local Git
repository. The -m flag allows you to add a commit message
describing the change.

Step 4: Create a New GitHub Repository


1. Go to GitHub and log in to your account.
2. Click “New” to create a new repository.
3. Name the repository (e.g., git-github-workflow).
4. Do not initialize the repository with a README (since we already have
files locally).
5. Click “Create repository”.

Step 5: Link the Local Repository to GitHub and Push


1. Copy the repository’s URL (available on the GitHub repository page).
2. In your terminal, add the GitHub repository as a remote:
bash

git remote add origin <repository_url>


o Replace <repository_url> with the URL you copied from GitHub.
o Explanation: This command links your local Git repository to the
GitHub repository, naming it origin.
3. Push your local commit to GitHub:
bash

git push -u origin main


o Explanation: git push uploads the commits from your local
repository to the GitHub repository. The -u flag sets origin main as
the default remote branch for future pushes.
4. Verify the upload by refreshing the GitHub repository page; index.html
should now be visible.

Step 6: Create a New Branch and Make a Commit


1. Create a new branch called feature-update:
bash
git branch feature-update
o Explanation: This creates a new branch called feature-update to
isolate changes.
2. Switch to the new branch:
bash
git checkout feature-update
o Explanation: git checkout switches the working branch to feature-
update.
3. Edit the HTML file:
o Open index.html in your text editor.
o Add a new line under the <h1> tag:
html
<p>This is a new feature update in the feature-update branch.</p>
o Save the file.
4. Stage and commit the changes:
bash
git add index.html
git commit -m "Add feature update paragraph to index.html"
Step 7: Push the Branch to GitHub
1. Push the new branch to GitHub:
bash
git push origin feature-update
o Explanation: This command uploads the feature-update branch to
the GitHub repository, allowing others to view and collaborate on
this branch.
2. Verify the branch on GitHub:
o Go to your GitHub repository page.
o Click on the “Branches” tab, where you should now see feature-
update listed as a branch.

Summary of Commands
Command Purpose
git init Initialize a local Git repository
git add <file> Stage a file for commit
git commit -m Commit staged changes with a
"message" message
git remote add origin Link local repository to
<url> GitHub repository
Push commits to GitHub’s
git push -u origin main
main branch
git branch
Create a new branch
<branch_name>
git checkout
Switch to the specified branch
<branch_name>
git push origin
Push a branch to GitHub
<branch_name>
By following this step-by-step guide, interns will get hands-on experience
with fundamental Git and GitHub commands, learning to organize code,
manage branches, and simulate collaborative workflows effectively.

You might also like