GIT and GITHUB
GIT and GITHUB
to Git &
Version
Controlling
Introduction to Version
controlling
Introduction to Version controlling
GIT is,
Open Source under GNU general public
license.
Distributed( allows each developer to have a
local copy of the entire repository, enabling
them to work offline and commit changes
independently)
Introduction to Version controlling
GitHub is a cloud-based platform built around Git, providing a hosting service for Git
repositories.
Since GitHub is a web-based service, it requires an internet connection to interact
with it. GitHub relies on Git.
Introduction to Version controlling
https://git-scm.com/
Setup GIT
https://github.com/signup?source=login
Activity 1
1. Download and install git into your PC.
2. Create a GitHub account(if you don't have a one already).
3. Make sure your github account is secured with multi-factor authentication
methods.
Setup GIT
Then tell your local git application who you are using these
commands,
Repository : The Repository is where Git stores all the metadata and object
database for your project. It includes the entire history of your project, all
commits, branches, tags, and configuration.
Repository is typically located in the .git directory within your git directory.
GIT Basics
git init :
To initialize the working directory to a git repository, we can execute this
command in our working directory,
$ git init
GIT Basics
git status :
To check the status of the git repo, use this command.
$ git status
GIT Basics
git add :
git add is used to add our changes to the staging area.
This command tells Git to include these changes in the next snapshot
(commit) you create.
Staging area : An intermediate area where Git stores changes that are ready to
be committed. It acts as a checkpoint between your working directory and the
repository's history.
GIT Basics
git commit :
Used to save changes to your local Git repository.
It takes a snapshot of the changes you've staged (using git add) and stores
them in the repository's history.
git commit only commit changes
in the staging area.
You must provide a commit message to describe the changes. This message
helps you and others understand what the commit does.
$ git commit -m <commit_message>
GIT Basics
git log :
Used to display the history of commits in a repository.
This command is essential for reviewing changes, understanding the project's
history, and tracking down when specific changes were made.
Commit hash
Author details
Time stamp
Commit message
GIT Basics
1.To see the last n commits 2. To see one-line summary of commit logs
git log -n <number> git log --oneline
git diff :
Used to show the differences 1. Compare changes between two commits
between various states of files in a $ git diff <commit1_hash> <commit2_hash>
repository.
It helps you compare changes in
your working directory, staging area,
and commit history.
This command is particularly useful
for understanding what has been
modified, added, or deleted before
committing changes.
GIT Basics
2. Check differences between your working directory and the staging area.
(use git diff command without any arguments)
$ git diff
Summary
1.git init
2.git status
3.git add < . | directory_name | file_name >
4.git commit -m < commit_message >
5.git log < -n number | -oneline | --author=author_name >
6.git diff < without any params | commit1_hash commit2_hash >
Activity 2
1. Create a folder named 'git_act'. Initialize that folder as a git repository.
2. Create a text document with the name of 'hello.txt'. Add the text line "Hello,
World!" in to that text file.
3. Create a html file. Add some basic html structre. In the body, create a
heading(h1) with the text of "Hello, Git".
4. Check the changes in the repo using an appropriate git command.
5. Add only the html file to the staging area. Then commit only the html file to
the repo using the commit message "html file added".
6. Check the repo status.
7. Again change the html file's heading text from "Hello, Git" to ""Hello, HTML!"
8. Stage all unstaged changes, all at once.
9. Commit them using the commit message "HTML modified and text file
added".
10. Check the previous commits using an appropriate git command.
Introduction to GitHub
Introduction to GitHub
ORIGIN : The default name Git gives to the remote repository from which you
originally cloned your project.
Ex: git push origin main
git clone :
Basic Syntax,
git clone <repository-url>
Ex:
git clone https://github.com/user/repo.git
Introduction to GitHub
when you clone a remote repository, Git automatically names that remote
'origin' by default.
Part 2 :
1. Try to clone this repo into your local machine using an appropriate git
command.
https://github.com/ranga1729/git_cheat_sheet.git
Introduction to GitHub
Branches
Branch is an independent line of development within a Git repository. It
allows multiple people to work on different features, bug fixes, or experiments
without affecting the main codebase.
Why Use Branches?
Isolate Changes - Work on a new feature
without modifying the main code.
Collaborate Easily – Teams can work on
different branches and merge their
changes later.
Safe Experimentation – Test new ideas
without breaking the working project.
4. Delete a branch
$ git branch -d <branch_name>
Introduction to GitHub
Merging Branches
Merging is the process of combining changes from one branch into another.
Whenever isolated work is completed and ready for integration, we can
merge.
Introduction to GitHub
Common base : Shared ancestor commit, of both branches before they split
up.
Feature tip : Latest commit of the feature branch.
Main tip : Latest commit of the main branch.
Introduction to GitHub
Main
Branch
Feature
Branch
Pull Request(PR)
A Pull Request (PR) is a feature in GitHub that allows developers to propose
changes to a repository before merging them into the main branch.
It acts as a code review request, where team members can review, comment,
and approve changes.
Introduction to GitHub
Ex:
two people edit the same line in a file
when one person deletes a file that another has modified.
GitHub Interface...
git reset
Used to undo changes.
git reset
Syntax,
$ git revert <commit-hash>
revert vs reset
Affects Safe for
Effect History Shared
? Branches?
Ex:
Ignore everything
/docs/*
in the a directory.
!/docs/README.md
Specifically saying
not to ignore this file
The .gitignore file only affects untracked files. It won't remove files already
tracked by Git.
Best practises in Git
Write clear commit messages
Use present tense imperative mood.
ex: "Add feature"
Make small, atomic commit messages.
Each commit should represent single logical change.
Don't include unrelated changes to the commit.
Create a new branch for each feature, bug fix or experiment.
Use descriptive branch names.
ex:
feature/login
bugfix/header-stylings
Best practises in Git
Frequently sync your branch with the main branch (e.g., main or master)
to avoid merge conflicts.
Review Changes Before Committing.
Use git status or git diff