0% found this document useful (0 votes)
161 views20 pages

Git

The document provides an overview of Git including what it is, how it works, and how to set it up. Git allows for version control and collaboration on code through local repositories that can be synced to remote repositories. It explains problems that version control solves and compares Git to SVN.

Uploaded by

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

Git

The document provides an overview of Git including what it is, how it works, and how to set it up. Git allows for version control and collaboration on code through local repositories that can be synced to remote repositories. It explains problems that version control solves and compares Git to SVN.

Uploaded by

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

Git

-- What is Git

-- SVN Vs Git

-- Git Architecture

-- Installation Git on your machine

-- Create Account in Github

--Crete a new repository in Github

--Create a Git project in local

-- Push to Github our new project.

What is Git?

Git is a version control system, but what is version control system?

Let's look at the problem #1 that we get when we do not use version control
system, so let's say....

you have a computer and you are writing code on that computer, so the code files
will be saved locally on that computer's hard disk, but what if your hard disks
crashes, or your computer has some kind of problem in that case whatever the
code you have written is lost..

Problem #2

So Problem #2 is if multiple people wants to contribute the code, then they can't
do it if it is on one computer.

let's say 2 people have they own computer's and they want to work on same code
how do they do that, they have to transfer files using pen drive from one
computer to another and they have to code merge manually.
Problem #3

Problem #3 is there is no tracking of versions.

Let's say you have code and it is working fine, and you are making on
announcement like fixing some bugs, you made some changes and let's say it
does not look good. so you want to go to previous version of your code but there
is no way if you have code on your computer because the simple file system does
not give you version tracking.

* so you have all these kind of different different issues especially when you
working on bigger code of sample project.

-- version control system solve all these problems.

-- Let's look at all these issues one by one.

Problem #1: Solution:

In version control system the code is saved on cloud, cloud is basically some
remote machine, which is much better than your local computer and it is
guarantee that you will never loose code from cloud.

so you store your code on the cloud and then you can able to see the files on your
local computer that way if your hardware problem or computer breakdown you
get your code back from cloud.

So problem #1 fixed.

Problem #2 Solution:

Multiple people contributing the same code, you have golden copy of your code
on the cloud so that multiple people can work on form their individual computers
and ten whenever they are adding to push the changes, they will push it to the
cloud.
--Now let's say if they are working on the same function here there is a python
function def sum(a,b): and they both are making changes to the same function
then merge tools available which can help you resolve the conflict.

Problem 3# Solution:

Problem 3# is version tracking, in version control system you have all your
changes tracked on through different versions of the same file.

Let's say you have file Foo.py which has function called sum(x,y); it just returns
a+b, but if you make changes later on and if you save it, it will be saved as version-
2, version-3 and so on.

so at any given point of time, you feel like want to go back to the previous old
version which was working okay, you can do it very easily.

** Let's discuss the version control system "SVN" first

For example This is my Central Repository means all people finally working on the
code, that code will go for the Development and Testing, QA environment and
Production environment , okay, whatever we have remote server is there that
remote server will go for the final deployment.

-- if i have different peoples are working on this code, okay for the same project,

now what happened in SVN, they don't have to see in other codes, okay, they
have only the snapshot of the code, full project won't have in the developers
machine.

and also what are the operations they want to do they will depends on the
internet and they will go for the every commit for the server, now SVN is the
remote server, every commit like as

gitadd

commit
check

push

log

what are the operations they are going to do, what are the commands they run
everything will work on the Remote server not in the local, that's why 10 people's
are working on the same project , all are hitting every time then the server may
be going to down or crashing, that type of issues we are facing with the SVN and
also every time when the people commit any command running the internet is
mandatory in this sub version control system.

SVN:

** Internet connection is required for developer server to Remote Server.


When it comes to Git:

As you can see in the above diagram, every programmer maintains a local
repository on its own, which is actually the copy or clone of the central repository
on their hard drive. They can commit and update their local repository without
any interference.

They can update their local repositories with new data from the central server by
an operation called “pull” and affect changes to the main repository by an
operation called “push” from their local repository.

The act of cloning an entire repository into your workstation to get a local
repository gives you the following advantages:

 All operations (except push & pull) are very fast because the tool only
needs to access the hard drive, not a remote server. Hence, you do not
always need an internet connection.
Committing new change-sets can be done locally without manipulating the data
on the main repository. Once you have a group of change-sets ready, you can
push them all at once.

Since every contributor has a full copy of the project repository, they can share
changes with one another if they want to get some feedback before affecting
changes in the main repository.

If the central server gets crashed at any point of time, the lost data can be easily
recovered from any one of the contributor’s local repositories.

Git Download and install:

1. Open the Git website.


2. Click the Download link to download Git.
The download should automatically start.
3. Once downloaded, start the installation
from the browser or the download folder.
4. In the Select Components window, leave
all default options checked and check any
other additional components you want
installed.
5. Next, in the Choosing the default editor,
used by Git unless you're familiar
with Vim we highly recommend using a
text editor you're comfortable using. If
Notepad++ is installed, we suggest using it
as your editor. If Notepad++ is not
installed, you can cancel the install
and install Notepad++ and then restart the
GitHub install.
6. Next, in the Adjusting your PATH
environment, we recommend keeping the
default Use Git from the command line
and also from 3rd-party software as shown
below. This option will allow you to use Git
from either Git Bash or the Windows
Command Prompt.

7. Next in the, we recommend leaving the


default selected as Use OpenSSH.
8. Next, in Choosing HTTPS transport
backend, leave the default Use the
OpenSSL library selected.
9. In the Configuring the line ending
conversions, select Checkout Windows-
style, commit Unix-style line
endings unless you need other line endings
for your work.
10.In the Configuring the terminal emulator
to use with Git Bashwindow, select Use
MinTTY (the default terminal of MSYS2).
11.On the Configuring extra options window
leave the default options checked unless
you need symbolic links.
12.Click the Install button
13.Once completed, you can check the option
to Launch Git Bash if you want to open a
Bash command line or, if you selected the
Windows command line, run Git from the
Windows command line.
Set Up a GitHub Account:

You will need a GitHub account to create a GitHub repository.

Go to https://github.com/join

1. Type a user name, your email address, and a password.


2. Choose Sign up for GitHub
3. Enter user name and email,password then click on verify.
4. select the free version
5. just hit continue and skip the step
6. verify your email address
7. In Github account, click on + symbol and click on create new reposity.
8. Repository name is jmsgit
9. select either public or private
10. click on create Repository.

-----Then open Gitbash----

--create directory as jmsgit

$ mkdir jmsgit

--change directory to jmsgit


$ cd jmsgit

--to check files inside directory.


$ ls -ltr
total 0

--to check hidden files.

$ ls -a
./ ../

--to create file and write content inside


$ cat > sample.txt

do $ ls -ltr- to check the file.

--to check the content inside file.


$ cat sample.txt
first git project

--So you have a directory that contains one file. Run git init in order to create a git
repo from that directory.
$ git init
Initialized empty Git repository in C:/Users/basani/jmsgit/.git/
* we did git init means we are initializing our git file as git project then .git folder
has been created for us.

--do ls -ltra then See .git file has been created right when we have .git file that
project is called as git project.

$ ls -ltra
--then open git bash and do git add
$ git add .

$ git commit -m "This is my first commit"


$ git config --global user.email "[email protected]"
$ git config --global user.name "srinubasani"
--if you want to see your configuration:
$ git config --list

$ git commit -m "This is my first project"

$ git push origin master


fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

--Please make sure you have the correct access rights


and the repository exists.

$ git remote -v

--then go to github account and copy the git remote add origin command and
paste in gitbash

$git remote add origin https://github.com/srinubasani/jmsgit.git

$ git remote -v
origin https://github.com/srinubasani/jmsgit.git (fetch)
origin https://github.com/srinubasani/jmsgit.git (push)

$ git push origin master

--Now go to Github account Let's check files are pushed or not

--now refresh the browser okay, see the Sample.txt has been reflected here.

---now let's add one more file in Github account.

$ cat > Helloworld.sh


echo Helloworld
$ cat Helloworld.sh
$ git add .
$ git commit -m "i added a new script" -a
$ git push origin master

** lets go and check Github account Helloworld.sh file also reflected there.

Git Merge Conflicts

Merge conflicts: Now i have a project in my Remote repository this is my Github


and inside github i have jmsgit project.

Github

--Developer A and B is working on same file, that is either class, shell script,
python script whatever it may be.

--for example that file name is Sample.txt now what happened these two
developers are taking latest code and pull this code.

--now in the Sample.txt file what are the content is there like as below
sample.txt----file
this is sample----content

now what happened is developer A has written the code in the sample.txt file like
this is my second line.
Sample.txt----file
this is my second line--content
and developer B has pushed to github so that sample.txt file contains 2 lines but
developer A is having only first line like as this is sample.
and developer A also added one line like as this is my code and developer A is
trying to push this content to github but github say's Mr. developer A you did not
have latest code, first pull latest code using git pull command, once you pull the
code and try to add code in second line but developer B already added code in
the second line.

** In this scenario merge conflicts will come to the picture.

Note: So what we will do is will do it git status so that merge conflicts file will
appear then we have to open this file in vi editor and remove unwanted
characters and next push this code to github.

Note: If both developer's are working on the same file and same line of code then
merge conflicts will be appear.

--Now go to gitbash and do ls -ltra

--let's open Helloworld.sh file


$ vi Helloworld.sh --- see this file is having only one code that is echo Helloworld
--Now other developer is working on your file in your Github.
opengithub--Repositories--click on jmsgit--click on Helloworld.sh--click on pen
button to edit--In second line add something like echo "edit hands on"--click on
commit changes.
-now go to gitbash
-open Helloworld.sh: $ vi Helloworld.sh
write the some code in 2nd line like as echo "i am from hyd"
--save it
--$ git add Helloworld.sh
--$ git commit -m "added 2nd line from local" -a
--$ git push origin master
Note: we get error like first pull the latest code and do changes then do commit.
-- $ git pull
-- $ git pull origin master
now see what it is showing merge conflicts with Helloworld.sh file.
-- $ git status
-- $ cat Helloworld.sh ---------------to check the content
now see echo Hello world first line of code is same, now see second line echo
"iam from hyd" has written by other developer and one more developer has
wrote code as echo "edit hands on"
--- $ vi Helloworld.sh
now remove unwanted characters and do the save.
-- $ git add Helloworld.sh
--$ git commit -m "Resolved conflict" -a
-m means message and -a means message is applicable for all files.
-- $ git push origin master
Let's open github account and check the repository now all the codes will be
available.
--What is Git branch
--when we will use branches
--how to create a branch in a local
--how to list all branches
--how to switch one branch to another branch
--how to push code into a new remote branch
--how to merge new branch to master branch
--how to delete branch in locally
--how to create branch in Remote
--how to delete a remote branch through command line.

**first of all what is git branch, when we are using git branch let's discuss these
things, basically this is my remote repository, what is the default branch we have
master, master is the default branch, this master is having all complete projects.
--Now what happened i don't want to modify master branch, some issues came in
the production, okay what they will do is they will create one bugfix branch, after
creating the new branch the complete project will come up here also.
--now what are the changes you want to do, you can do with the bugfix branches,
now modify the code and push the code to bugfix and test this code then finally
this merge bugfix branch to master, after merging what are the code in the bugfix
branch that will be available in the master branch, now this bugfix branch don't
required right because putting one project with two resources is not good then
what we will do is we delete bugfix branch.
--different different teams they create branches for them like as UI developer,
database developers, db backend developers finally they will merge into the
master branch.
Explanation: I have project, under this project i have master branch, master
branch is having complete code, what happened now i deployed code into
production, here what happened the code is not working properly, okay that they
won't modify the code directly into the master branch, what they will do, they will
create branch called bugfix, after creating bugfix branch the code copy is available
into this branch, now what happened will modify code into bugfix branch and test
this code into master branch and master branch is having the latest code then we
will delete bugfix branch.

--now open Github account


--check the branch we are having only one branch that is Master.
--now what i will do is will open git bash
--change directory into jmsgit project: $ cd jmsgit/
-- do $ ls -ltr
sample.txt and Helloworld.sh files are there.
--now let me list out how many branches are here
$ git branch --list
i have only one branch.
--if you do git branch -a it will show all branches remote repository as well as local
repository.
$ git branch -a
---now what is will do, i need to create one branch.
$ git branch bugfix
--now lets list out the branches
$ git branch --list
it will show the bugfix and master, now where we are, we are in master branch ,
that green color indicates we are in master branch not in the bugfix.
-- now i want to switch to bugfix.
$ git checkout bugfix
now we have switch to bugfix branch where iam in now bugfix branch.
--now let's se ls -ltr
$ ls -ltr
now whatever the code is available in the master branch, every code will be
available in the bugfix branch also.
--now in the bugfix branch i want to create one file to fix my issue.
$ vi newbugfixfile.txt
iam a fixed file
:wq
--now do ls -ltr
$ ls -ltr
now iam having new file.
--now i want push this code to Github
$ git push origin bugfix
--now go to Github repository and check how many branches are there, let's see
we have two branches now, check how many files are there in master branch and
how many files are there in bugfix branch.
--if you check in the bugfix branch newly created file called newbugfixfile.txt is not
there because we did not do git add and git commit., okay after changing the file
what we need to do is we have to do git add,commit,push.
$ git add .
$ git commit -m "new bugfix file" -a
$ git push origin bugfix
now check in github bugfix branch that newly created file called newbugfixfile.txt
is available now.

---now i want merge this code to master branch.


***now iam in the bugfix branch i want switch to master branch form there i
need to merge master branch to bugfix branch.
$ git checkout master
now iam in the master branch , in master branch the newbugfixfile.txt is not there
which we created inside bugfix branch, if you want to see that code in master we
need to merge.
$ ls -ltra
$ git merge bugfix
--net do ls -ltra
$ ls -ltra
now that newbugfixfile.txt is available in mater branch also in locally.
$ git add.
$ git commit -m "merging" -a
$ git push origin mater: now the newbugfixfile.txt is available in master branch
which we created under bugfix branch.
$ git branch --list

$ git branch -d bugfix


$ git branch --list
locally bugfix branch has been deleted when i check in github it is having, now i
want to delete in remotely(github account)
$ git push origin -d bugfix
now bugfix branch is deleted in github also.

----Now open github repository and create one branch called future
--now future branch also having same files right, now how can i pull future branch
to my local.
--open gitbash
$ git branch --list
we are having only one branch that is master
$ git branch -a

-------Now how can i pull future branch to my local.


$ git fetch
$ git branch -a
--now i want switch to future branch
$ git checkout future
--now iam in future branch and check how many branches are there.
$ git branch --list
iam having two branches now iam in future branch
$ ls -ltra
now iam having complete code which is available in future branch also
$ vi newfuture
create a new future
:wq
$ git add .
$ git commit -m "new future" -a
$ git push origin master
Note: Earlier i have 3 files in future branch in github repository after
implementing this i got 4 files., let's refresh github repository now newfuture file
also got it.
-----now if you see master branch it is having only 3 files.

---now i want to merge this future branch to master branch


$ git checkout master
now iam in master branch, master branch is having 3 files and future branch is
having 4 files.
$ git merge future
$ git push origin master
now let's refresh in github master branch and see now we got 4 files.
$ git log master

You might also like