KEMBAR78
Git | PDF | Computer File | Directory (Computing)
0% found this document useful (0 votes)
12 views56 pages

Git

This document provides a comprehensive guide on using Git and GitHub, covering commands for initializing repositories, tracking changes, and managing branches. It explains how to commit changes, push to remote repositories, and handle pull requests for collaboration on projects. Additionally, it emphasizes best practices for branching and managing contributions in open-source projects.

Uploaded by

saswatseth.mail
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)
12 views56 pages

Git

This document provides a comprehensive guide on using Git and GitHub, covering commands for initializing repositories, tracking changes, and managing branches. It explains how to commit changes, push to remote repositories, and handle pull requests for collaboration on projects. Additionally, it emphasizes best practices for branching and managing contributions in open-source projects.

Uploaded by

saswatseth.mail
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/ 56

GIT

Notes:

cmd - git to get some git info, implies git is well installed
ex:git
cmd -cd ‘folder_name’- to take the current scope to a folder
ex:cd desktop
cmd-ls to list all the files in the folder
ex: ls
cmd- mkdir ‘folder_name’ -to make a file in the folder
ex: mkdir project;
Now that we have created the project, we want to maintain the history of the proj using git, we create a new file, we
make changes or delete the file, all of these history is in another folder that git provides us, this is knows as a git
repository and it names as .git. Dot in linux and macos, etc are files that are hidden. So how can we get this folder in
our project
Hence
cmd- git init
This init an empty git repo/folder

Now if we
cmd- ls in project directory
the .git folder wont be visible but its there

Hence we can,
cmd - ls -a
which means to list the hidden files too

To check whats in the .git folder


we can
cmd - ls .git

now lets make a change in the repo,


cmd - touch names.txt
this creates a txt file named names.txt in the project folder.

However the world doesn’t know that we made this change so how do we see what changes are made in the project
that has not been currently saved in the hist of the project

hence

cmd- git status


We see names.txt but it also shows untracked which means if we share this project or git repo, no one knows that
names.txt files was added by us on 17/2/24.This command tells us that these are all the changes which arent in the
history of out project.

Hence to make untracked files, we save their history


cmd - git add .
which adds everything which is untracked in the current directory to the staging area so that they can be further
saved
cmd - git add names.txt
we can also write indiv names of the file, in case we have a few files but I want to maintain the history of some files.

Now to permanently save the changes in git history, so as to really make the change
Cmd - git commit -m “names.txt file added”

Here -m implies the message,we passed this “names.txt file added” as the message

0 insertions and 0 deletions of lines of code coz we actually havent made any difference we havent done both of
them on the file.

Ok so now since we havent made any change after it hence if


Cmd - git status

We see this, which means no other changes which is supposed to be added

Now lets add something to the file, which is a change, for that we will

Cmd - vi names.txt
We enter into vim text editor, We have learned that Vim is a powerful text editor popular among developers. It's
based on shortcuts, called the Vim language, which can make coding and writing faster and more efficient. With Vim,
you can jump to any specific text position and rapidly make precise edits.

To jump to its action mode we can click esc


Press Esc key and type :w to save a file in vim. One can press Esc and type :wq to save changes to a file and exit from
vim. Another option is to press :x.
We can either double click on names.txt

Or we can write
Cmd- cat names.txt

Now
If we write
Cmd-git status we will see some changes have been made
It shows modified: names.txt

Now lets take a picture of it(metaphorical)

Cmd- git add

The warning is for this

Cmd-git status

Now as we see the change is staged, now we click a pic to save it permanently

Cmd-git commit -m “changes are made”


This can be done to make the changes

Now if we want to unstage the changes, i.e revert the changes made by git add
We can
Cmd - git restore --staged names.txt

Now if we
Cmd-git status
We are reverted back to the state before Cmd- git add

Now if we actually
Cmd- git add .

Cmd - git commit -m “names.txt file modified”

Ok now how can we see all the changes ever made to the, all the commits that were ever made or all the history of
the project

cmd - git log

See how the latest change is being shown in the first part

Now we delete the names.txt file

Cmd- rm -rf names.txt

Now if git status


Now to stage this we do git add .

And then git commit -m “names.txt file deleted”

Now git status

And then git log

Ok now what if we want to delete the commit as maybe we did this by mistake from the history of the project, and
fyi we cant delete commits from the middle as each of them has a hash id which is built on top of each other.
So if we want to revert our projects state to another previous version we have to remove the commits before the
commit we want our project to revert back to

So we use

Cmd- git reset ‘hash id of the commit we want to revert to’


Here we want our project to be what it was on sat feb 17, 23:53:49
hence we can see we have to delete the first two commits
For this

Cmd- git reset 6dd866af0a2e9e5f670b715dfa2b65c728f997e1


Now if we git log

This means the previous changes in prev commits are now once again ustaged area

Hence if we git status now

Now if we deleted this file by mistake then we can restore it or we can put it in the stash area
Ex: we don’t want to delete these changes and we just want to put them aside while we work on a clean code base.
Basically its like we have a few people to take photo of but instead of wanting to stage them we want to backstage
them so that when we are ready for them they can be called.
Another example would be what if we were working on a project and we had a new feature in mind but we want to
start off with a clean code base for it. All the changes that we have made, we want to remove those but we also
don’t want to save these changes/progress as a separate commit. So this is way to put all of our work somewhere
without making a commit/without making a history in the project and whenever we want it back we can get it back
hence
First we add the people who we want to be in the backstage first on the stage with git add.

Then git status and we see the deleted: names.txt is tracked now
So here this deleted : names.txt is a change and we want it to be in the stash(the backstage area)

so whenever we want these changes back we will bring them back


Ex: we made another change
cmd- touch surnames.txt
It says it renamed it surname.txt which is smart of them.

We havent commited hence the old one

So we make more changes

Two changes

So now we don’t want to commit these changes and we also don’t want to lose these changes hence we send them
to backstage wheneever we want them we can bring them back.
Hence we do this

Cmd- git stash


Now when we git status

now out project folder is exactly how it was on sat feb 17 23:53:49
As we reverted it back to a previous version hence we see that there is nothing in the file

as in out first commit we didn’t add anything into it

I added the names of my family members after the first commit and in the second commit

And to get back the changes from the stash


we have to

Cmd- git stash pop

And now if we want to clear the stash

As we popped these from the stash we add them back to staging area with git add .
And then we do git stash

And then to clear stash


We
Cmd- git stash clean

So now we will remove the people from the backstage forever

GITHUB

1. Hosting our own project on github, personal/local project. We want to push those into github or share
it with other people

so we create a repo in github and now we want the url of the github repo to be attached to out project/ to out local
project
Our url
now we want the url of the github repo to be attached to out project/ to out local project

hence

Cmd- git remote add origin https://github.com/Cyberoctane29/GITHUB-Practice.git

Git means git command

Remote means we are working with urls

Add means we are adding a new url

Origin means what is the name of the url that is going to be added, so here origin is the name of the url.
Ex : we don’t remember people’s ph no we save it with some name

This can be any name but for best practice and convention we should name it origin

By convention all repositories and folders ex: GITHUB-Practice.git


That are in your personal account Cyberoctane29, they have a name of origin.

Now if we
Cmd- git remote -v
It will show us all the urls attached to this folder ‘project’

Now if we refresh our github would we see names.txt, no as we havent shared the changes on this url
So the command for this is

cmd- git push origin master

Push means push the changes


Origin is the url we want to push
and master is which branch we want to push
Commit is also here

Master was known as main for naming convention, it says main newly but previously it used to be called master.

What are branches?

Lets make some random commits


We added something to roll_no.txt

Now we commit
But wait we didn’t git add. And we commited hence this thing was the result
This is the latest commit on our remote branch

As we are creating git commits we see commits branch out subsequently which would make it a directed acyclic
graph
By default the name of this branch(subsequent commits) is known as main, previously it was knows as master.
Use of branch
when we are adding a new feature or a resolving a bug, we should create a separate branch.

Reason for this will be said

So if we display the .git folder’s content


What is the head?
So the point is in branches we should never commit on the main branch/master branch, because is open source
project
So if we go to any open source proj on github ex: react and go to the main folder the facebook/react-native

So we see it’s on the main branch

These are all the code which is actually being used by people, the main branch is being used by people so that’s the
reason we don’t commit to the main branch as our code is not finalised so it might contain any other errors that’s
why all the code we are working upon and is not finalised that we are working on should go to a serparate branch so
the users arent affected.
To create a new branch

Previous main branch

Cmd- git branch feature


So we see a new branchis created called feature

Now to point the head to the new branch

Cmd-git checkout feature

So now we see the * which was on main Is now on feature which shows now our head is pointing to feature
Head is a pointer and it means all the new commits done by us will be added to this feature branch
Now on subesequent commits

Feature will branch out

Now we are not the only one who is contributing to the code base, so many people contribute to a code base
So while we are working on our branch/while we are working on our issues someone else added to the main branch
so now the main branch goes its own way and our work goes on side by side

Now we
Cmd- git checkout main

Now the star is on main branch so any commit will be on it but now our branch’s code cant be seen as they are not
part of the main branch, these changes arent the part of it
So to really integrate your finalised changes so the people who maintain the project will merge it with
Cmd-git merge feature

Now people can see the code as its part of the main branch

Now lets push our commits to our origin master


We see head is on the master branch

And the url we added is also here which means there are no commits above it which are extra which arent already
on the url

2. Working with exisitng project on github


This is commclassroom organisation and comclasroomOP is there project, now to make contrib or changes to this
project ,anyone who doesn’t have access to it shouldn’t be allowed to change any file in the folder, currently it has a readme file
because we don’t have access to the organisation,so if we don’t have permission for this then how can we contrib

We create a copy of it in our own account


hence
so can fork it

Now
As this folder now is in our account we can do anything with it

Hence now we

And any folder that starts without own account

the name of that will be origin

Hence now we do

Cmd-git clone https://github.com/Cyberoctane29/commclassroomOP.git


Accessing the files in it

We have to fork a project if we want to make any modif to it we cant do it to the main project, so our changes on the forked
code wont be reflected in the main project until and unless the people who have access to the project can merge our
code/approve our changes via a pull request.

Now origin url is basically our own account


https://github.com/Cyberoctane29/commclassroomOP

But from the url we have forked this project is known as upstream ‘url’ by convention, hence we can add another url
Cmd-git remote add upstream https://github.com/commclassroom/commclassroomOP.git

Origin is our personal, upstream is where we have forked from


Basically we cloned the project

Now we shouldn’t commit any changes in the readme on the main branch

We should make another branch

Now we should point the head to saswat branch


Cmd- git checkout saswat
All the commits we make now will go to the saswat branch

Now if we modify the readme.md using vim

And now we git status

And now we commit to this branch


Cmd - git add .

Cmd - git commit -m “README.md is modified”

This is the main branch


Not changed only till here c3164c5542fc3e77e5861eff83938032d5b35f3a we have the commits
And this is our saswat branch having a commit

We want to push this saswat branch to the main project’s main branch being managed by someone named X, so I who owns

The saswat branch will request the X the person who is responsible for the main project

Ex:

Kubernetes website
So they want to make a change so they created a fork of it names kunal-kushwaha and this fork has a branch called sig-usability-
blog,

which contains these changes

So they requested the people who maintain the project so that can they merge the changes into the kubernetes main project’s
main branch so that people can see those changes so after that they will give suggestions and rthey will run some tests and
they(Kunal) made some changes and atlast their pull request was merged.

Why we need another branch


Lets try to push saswat branch to github

Cmd - git push upstream

This will give us error as we don’t have access to the main folder or repo

We have the access t o the origin url

Cmd - git push origin saswat


Origin is our own account
It shows this
we click on it
Whenever we add something here so CyberOctane’s commclassroomcopy fork has a branch called saswat

And it contains these changes

And we are requesting please merge these changes into the main branch of the project

Then we click on create a new pully request

The main project’s owner or the ones having access to it will get a notif
Then they can merge this pull request

but then if we make some new change and commit it and then write git push origin kunal, we notice that now it will not allow
us the create a new pull request and it will add the commits to the existing pull request
This is the reason we shouldn’t make commits on main branch

Ex: we are working on 10 features and for every feature we are creating pull request, how difficult would It be to review our
code to have the discussion, all the ten different things on a single pull request I.e why for every new feature, bug ,etc create a
new pul, and from this ex we saw if a branch already has a pull request it will not allow us to create a new pull request all the
commits would be added to the existing pull request

In simple lang
One pr means one branch
One branch can only open a pull request
If we want to open different pull requests for different features ina project which is recommended hence we need to create
different branches.
Any new thing we are working on we need to create a new branch in our local folder and make a pull request from it.
Making different pull request makes sense, coz in open source many people are working on their own indiv pr, it would be really
complicated for every pull requests would be in one pr/branch and many people that would have to collab in the pr.

If a branch already has a pull request then we cant make a pull request as every commit would be added into the same pr, hence
we can only open a new pr with new branches
Doubt from this part

Two files have been changed, names.txt has been added and readme has been modified by us
lets remove the commit of readme modification

we copy the one below it to remove it

cmd - git reset c3164c5542fc3e77e5861eff83938032d5b35f3a

Git add .
And then git stash
To make it go to some stash area

Git log

Now when we push we have to force push it because online repo contains a commit which our repo doesn’t and as commits are
interlinked so we have to force push it

Its not showing but the readme modif notif would have been still there
Like this

Right now the commit is still there hence

Cmd- git push origin saswat -f

Now we see our project is some commits behind to sync the main branch’s changes made into your forked main branch we can
use a few commands or just fetch upstream button
Basically the changes in commit in your main of forked should also have commit changes from the main project you have forked
from
If we git log our forked project we see one commit but in reality in upstream we see more commits so
First we fetch all of the changes

Cmd - git fetch --all --prune


Prune means to fetch the changes that were deleted

Then After pointing to the main once again


Then we reset it
Reset our main branch of origin to the main branch of upstream

Cmd- git reset --hard upstream/main

Git log
Now we have the main branch of our forked system exactly same as the one we have in upstream folder

Then we also have to

Git push origin main


To reflect the changes in out origin forked folder

Also
Cmd- git pull upstream main

Will internally do the same work just like git fetch

Now all the changes are in our local folder not in the upstream hence
Cmd - git push origin main
Coz our local folders main is in sync with the main branch-upstream branch hen lets sync the origin url then

Or we can just directly to fetch upstream and this all would be done

No doubt from here


1.Merge conflict
2.sqaushing our commits

whenever we create a new branch it should be created from the main branch so whenever we create it we should make sure we
are on the main branch

We switched to main
We created a branch temp whose name was given as temo by mistake then we changed its name and then switched
to it

We can also do more than one command at a single time like this

Git log
We want to merge all this 4 commits into a single commit

W can either revert back to the commit after which to the end we want to squash the comments, that would be

And then we after resetting we can just make the commit at once as all the commits are unsatged

Or we can

Cmd- git rebase -i c3164c5542fc3e77e5861eff83938032d5b35f3a

-i is for interactive environment


So now all the commits which were above it, the one we pasted
We can pick or squash

We can change it like

Pick ____1
S ____2
S____3
S____4
So we will change this

This will mean the three commits will be sqaushed to the one we left for pick which is the first one

If we wanted to merge the middle two commits,we could have done this

The middle to sqaush ones will be squashed to the above pick


Pick means we are picking this commit means we want it to be included

After we esc+:x we see


It allows us to edit a new message

Then again esc+:x

Git log
Commits are merged

if we git push origin temp


Which will make these files available over there
If we don’t want these files so we can remove them

We want to remove

This is the commit which created these 4 files

Hence asusual we first copy the has id of the commits below it

And we
Git reset --hard c3164c5542fc3e77e5861eff83938032d5b35f3a
Hard resets it with hard so we do it with caution
3. Merge conflicts

If we made a change in line number 3 and someone else did the same

so git gets confused to take which one


So git asks which change to take

So if changes on the same line is made by two people from two branches which are to be merged to the main branch via a
pull request then merge confict occurs
A merge conflict

So we have to resolve the conflict manually


So we do this

Mark as resolved
Commit merge

You might also like