0% found this document useful (0 votes)
23 views21 pages

Git and Github

The document provides an overview of Git and GitHub, detailing Git as a distributed version control system for tracking code changes and collaboration, while GitHub serves as an online platform for hosting Git repositories. It outlines the steps to install and configure Git, manage repositories, stage and commit changes, and utilize branching and merging features. Additionally, it explains the process of contributing to open source projects through forking, cloning, and submitting pull requests.

Uploaded by

abhay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

Git and Github

The document provides an overview of Git and GitHub, detailing Git as a distributed version control system for tracking code changes and collaboration, while GitHub serves as an online platform for hosting Git repositories. It outlines the steps to install and configure Git, manage repositories, stage and commit changes, and utilize branching and merging features. Additionally, it explains the process of contributing to open source projects through forking, cloning, and submitting pull requests.

Uploaded by

abhay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

GIT AND GITHUB

What is Git?
Git is a popular distributed copy based version control system.

It is used for:

●​ Tracking code changes


●​ Tracking who made changes
●​ Coding collaboration

It was created by Linus Torvalds in 2005

Principal Of Version control


Result = Start + Sum of changes

What does Git do?


●​ Manage projects with Repositories
●​ Clone a project to work on a local copy
●​ Control and track changes with Staging and Committing
●​ Branch and Merge to allow for work on different parts and versions of a project
●​ Pull the latest version of the project to a local copy
●​ Push local updates to the main project

Working with Git


●​ Initialize Git on a folder, making it a Repository
●​ Git now creates a hidden folder to keep track of changes in that folder
●​ When a file is changed, added or deleted, it is considered modified
●​ You select the modified files you want to Stage
●​ The Staged files are Committed, which prompts Git to store a permanent snapshot of
the files
●​ Git allows you to see the full history of every commit.
●​ You can revert back to any previous commit.
●​ Git does not store a separate copy of every file in every commit, but keeps track of
changes made in each commit!

What is GitHub?

●​ The key difference between git and github is that git is a free open source version control that
developers install locally on their personal computer while github is an online service built to run git
on cloud.
HOW TO USE GIT
STEP -1

Install git - Download

STEP -2

Git CMD is just like regular Windows


command prompt with the git
command. It lets you use all of Git
features through command line. Useful
if you are already familiar with Windows
cmd and you only work on Windows.

Git GUI is a Graphical User Interface


letting you use Git without touching
command line

Git Bash emulates a bash environment


on windows. It lets you use all git
features in command line plus most of
standard unix commands. Useful if you
are used to Linux and want to keep the
same habits.

STEP -3 - NOW CHECK WHETHER THE GIT IS INSTALLED OR NOT FOR THAT WE HAVE TO
CHECK THE VERSION OF GIT BY USING GIT COMMAND (git --Version)

If Git is installed, it should show something like this.


STEP -4 - CONFIGURE GIT
This is important for version control systems, as each Git commit uses this information:

Note: Use global to set the username and e-mail for every repository on your computer.

If you want to set the username/email for just the current repo, you can remove global

NOW WE CAN CHECK OUR CONFIG SETTINGS

git config –list


STEP -4 CREATING A GIT FOLDER

●​ mkdir makes a new directory.


●​ cd changes the current working directory.
●​ Now that we are in the correct directory. We can start by initialising Git!

STEP -5 INITIALISE GIT

●​ Once you have navigated to the correct folder, you can initialise Git on that folder:
●​ Note: Git now knows that it should watch the folder you initiated it on.Git creates a hidden
folder to keep track of changes.
●​ You just created your first local Git repo. But it is empty.
●​ So let's add some files, or create a new file using your favourite text editor. Then save or
move it to the folder you just created.
●​ For this example, I am going to use a simple HTML file like this:
●​ And save it to our new folder as index.html.
●​ Let's go back to the terminal and list the files in our current working directory:
●​ ls will list the files in the directory. We can see that index.html is there.
●​ Then we check the Git status and see if it is a part of our repo:
●​ Now Git is aware of the file, but has not added it to our repository!

Files in your Git repository folder can be in one of 2 states:

●​ Tracked - files that Git knows about and are added to the repository
●​ Untracked - files that are in your working directory, but not added to the repository

When you first add files to an empty repository, they are all untracked. To get Git to track them,
you need to stage them, or add them to the staging environment.
STEP -6 GIT STAGING ENVIRONMENT

In the staging environment we Staged files files that are ready to be committed to the
repository you are working on.

●​ For now, we are done working with index.html. So we can add it to the Staging
Environment:
We can Add More than One File
STEP-7- COMMIT

●​ Since we have finished our work, we are ready to move from stage to commit for our
repo.
●​ Adding commits keep track of our progress and changes as we work.
●​ Git considers each commit change point or "save point". It is a point in the project you can
go back to if you find a bug, or want to make a change.
●​ When we commit, we should always include a message.
●​ By adding clear messages to each commit, it is easy for yourself (and others) to see what
has changed and when.

●​ The commit command performs a commit, and the -m "message" adds a message.
●​ The Staging Environment has been committed to our repo, with the message:​
"First Change"
We can multiple time edit and modify and commit
Git Commit without Stage
Sometimes, when you make small changes, using the staging environment seems like a waste of
time. It is possible to commit changes directly, skipping the staging environment. The -a option
will automatically stage every changed, already tracked file.

☕️ - Check git log docs


Atomic commits are a way to make sure that each commit is a self-contained unit of work. This means that if one
commit fails, you can always go back to a previous commit and fix the issue. This is important for maintaining a
clean and organised history in your repository.

NOTE
We can check the status of our repository. But this time, we will use the --short option to see
the changes in a more compact way:

Git Commit Log


To view the history of commits for a repository, you can use the log command:
NOTE- To view the log in one line we can use flag git log --oneline

GITIGNORE

Gitignore is a file that tells git which files and folders to ignore. It is a way to prevent git from
tracking certain files or folders. You can create a gitignore file and add list of files and folders to
ignore by using the following command

GIT HELP
●​ git command -help - See all the available options for the specific command
●​ git help --all - See all possible commands
GIT BRANCHES

●​ In Git, a branch is a new/separate version of the main repository.


●​ Branches allow you to work on different parts of a project without impacting the main
branch.
●​ When the work is complete, a branch can be merged with the main project.
●​ You can even switch between branches and work on different projects without them
interfering with each other.
●​ Branching in Git is very lightweight and fast!

Head In Git
The head pointer to the current branch on which you are working. It points to the latest
commit in the current branchages.when you create a new branch it will automatically set as the
head of the branch.

●​ git branch - This command lists all the branches in the current repository.
●​ git branch bug-fix - This command creates a new branch called bug-fix.
●​ git switch bug-fix /git checkout bug-fix- This command switches to the bug-fix
branch.
●​ git log - This command shows the commit history for the current branch.
●​ git switch master/git checkout master- This command switches to the master
branch.
●​ git switch -c dark-mode - This command creates a new branch called dark-mode.
the -c flag is used to create a new branch
EMERGENCY BRANCH

●​ Now imagine that we are not yet done with hello-world-images, but we need to fix an error
on master.
●​ I don't want to mess with master directly, and I do not want to mess with
hello-world-images, since it is not done yet.
●​ So we create a new branch to deal with the emergency

Using the -b option on checkout will create a new branch, and move to it, if it does not EXIST
MERAG BRANCHES

Fast-forward merge

git merge bug-fixThis one is easy as the branch that you are trying to merge is usually
ahead and there are no conflicts.

When you are done working on a branch, you can merge it back into the main branch. This is done
using the following command

Not fast-forward merge


Sometimes we work on master branch and simultaneously on other branches, in this
process we sometimes do the similar commit in both the branches, so in this case we
cannot merge the branches by fast forword method because of the conflict and there are no
shortcuts to resolve them. You have to manually resolve the conflicts. Decide what to keep
and what to discard.

Managing conflicts
There is no magic button to resolve conflicts. You have to manually resolve the conflicts. Decide
what to keep and what to discard. VSCode has a built-in merge tool that can help you resolve the
conflicts. I personally use the VSCode merge tool. Github also has a merge tool that can help you
resolve the conflicts but most of the time I handle them in VSCode and it gives me all the options
to resolve the conflicts.

Overall it sounds scary to beginners but it is not, it’s all about communication and understanding
the code situation with your team members.
●​ Rename a branch
●​ git branch -m <old-branch-name> <new-branch-name>

●​ Delete a branch
●​ git branch -d <branch-name>

GIT DIFF COMMAND


The git diff is an informative command that shows the differences between two
commits. It is used to compare the changes made in one commit with the changes made in
another commit.

Comparing Staging Area with Repository


This command shows the changes between your last commit and the staging area (i.e., changes
that are staged and ready to be committed).
git diff --staged
Comparing between branches
This command compares the difference between two branches. For example:

Comparing Specific Commits:


This command compares the difference between two commits.
GIT STASH
Stash is a way to save our changes in a temporary location. It is useful when we want to make
changes to a file but don’t want to commit them yet. we can then come back to the file later and
apply the changes

Conflicting changes will not allow us to switch branches without committing the changes. Another
alternative is to use the git stash command to save our changes in a temporary location.

git stash- This command saves your changes in a temporary location. It is like a stack of
changes that you can access later
git stash save "work in progress on X feature" -You can also name the stash
by using the following command:
git stash list - You can view the list of stashes by using the following command:
git stash apply- You can apply the stash by using the following command:
git stash apply stash@{0}-You can apply the specific stash by using the following
command:Here stash@{0} is the name of the stash. You can use the git stash list
command to get the name of the stash.
git stash pop - This command applies the stash and drops it from the stash list.
git stash drop -You can drop the stash by using the following command:
git stash apply stash@{0} <branch-name> -You can apply the stash to a specific
branch by using the following command:
git stash clear- You can clear the stash by using the following command:
What is Github?
Github is a web-based Git repository hosting service. It is a popular platform for developers to
collaborate on projects and to share code. Github provides a user-friendly interface for
managing and tracking changes to your code, as well as a platform for hosting and sharing
your projects with others.

Some other alternative of Github are:

●​ Gitlab
●​ Bitbucket
●​ Azure Repos
●​ Gitea

But the mainstream popular tool these days is Github.

What is SSH
SSH is a secure shell network protocol that is used for network management, remote file
transfer, and remote system access.

SSH uses a pair of SSH keys to establish an authenticated and encrypted secure network
protocol. It allows for secure remote communication on unsecured open networks.

PUSH TO GITHUB
STEP-1 - Create a new Repo on your system first, add some code and commit it.
STEP-2- Check remote URL settingns This will show you the remote url of your repository.A remote URL is Git's
fancy way of saying "the place where your code is stored." That URL could be your repository on GitHub, or
another user's fork, or even on a completely different server.

Here we can see that there is no remote url on which we can push our code , so first we have to add a remote
repository by running the following command:

STEP-3- Add a remote repository by running the following command:

origion - This is used to refer to the remote repository in future or we can say that origin is the name of remote
repositery
STEP-4 - Now remote connection is established and you can now push code to remote repository by using the given
code
git push remote-name branch-name

remote-name- remote-name is the name of the remote repository in which you want to push
branch-name- branch-name is the name of the branch that you want to push.

git push origin main

Note:- To remove origin – git remote remove origin


Setup an upstream remote
Setting up an upstream remote is useful when you want to keep your local repository up to date with the remote
repository. It allows you to fetch and merge changes from the remote repository into your local repository.
Get code from remote repository
There are two ways to get code from a remote repository:
●​ fetch the code
●​ pull the code
Fetch the code means that you are going to download the code from the remote repository to your local repository.
Pull the code means that you are going to download the code from the remote repository and merge it with your
local repository.
HOW TO DO OPEN SOURCE CONTRIBUTION

Fork - A fork is a copy of a repository. This is useful when you want to contribute to someone
else's project or start your own project based on theirs.
Clone- A clone is a full copy of a repository, including all logging and versions of files.
git clone https://github.com/w3schools-test/w3schools-test.github.io.git
Push- Now we push them to our GitHub fork:
Pull Request- A pull request (PR) is a way for developers to propose changes to a Git
repository and request that they be merged into the main codebase. PRs are also known as
merge requests.

You might also like