Learn GIT Using GITHUB in 5 Minutes
Learn GIT Using GITHUB in 5 Minutes
GIT
WITH
GITHUB
IN 5 MINUTES
What is a Repository?
Git repository contains main project’s source code.
What is GitHub?
GitHub is an Internet hosting platform for software development and
version control using Git .
In order words GitHub is simply a website which holds the Git
repository which in turn holds the project’s source code.
In the next chapter we will learn how to add the GitHub repository
into our local machine.
Chapter 3 : Git clone
In the previous chapter we have successfully created our GitHub
repository . In this chapter we will learn how to get a copy of the
GitHub repository for our own local machine and have our own
local repository.
In order to do perform this task Git provides us with git clone
command and the syntax is:
git clone url, y ou can get the url from GitHub repository page
highlighted in the screen shot below.
Now open the directory or folder where you have cloned the GitHub
repository .
The hello_world directory shows.
For our local Git repository, let’s set up the name and email
address with the help of git config command.
In the next chapter we will learn how to add files into our local
repository and then push those changes into the main GitHub
repository .
Chapter 4 : Git add, Git commit &
Git push
Whenever you make any changes in the local repository , those
changes have no effect in the main GitHub repository . In order to
push those changes into the main GitHub repository we need to
follow few steps.
But before we learn how to do this task, first we need to understand
the difference between a working directory and local repository .
index.html
In order to add index.html into the GitHub repository , we need to
follow three steps:
Step 1: Use git add command to add the file from working
directory to the staging area . The syntax is git add filename
Step 3: Use git push command to push the changes from our local
repository into the main GitHub repository .
Refresh the GitHub website repository page and check for the
presence of the HTML file.
index.html file shows.
Now in the next chapter we will learn how to get the updated new
version of index.html from GitHub repository into our local
machine.
Chapter 5 : Git pull
In the previous chapter we have learnt how to add a file from our
local repository to GitHub repository. Now we will learn how to get
the latest version of a file from GitHub repository into our local
machine.
Add a line of code in index.html -> write the commit messag e ->
click on commit changes button as shown in the screen shot below.
Now GitHub repository contains the latest updated version of
index.html file. In order to get this new version into our local
repository git pull command is used.
In your local machine, open command prompt -> navigate to the
hello_world directory and type the command git pull
Refresh index.html file in our local machine and you will see the
updated version of index.html is added (the line git add
<i>filename</i> is present as shown in the screen shot below ).
Chapter 6 : Git Merge Conflict
Merge conflict happens in the scenarios in which two different
developers are working on the same file and on the same lines of
code. When this happen Git does not know how to fix the issue and
throws a merge conflict message and it is up to the developer to
resolve such situation.
Let us consider a programmer x is working on a file (suppose
index.html ) and made some changes in line 10 in his/her local
repository . Suppose there is another programmer y that made
some changes to index.html in the same line 10 and pushed those
changes into the main GitHub repository .
Now when programmer x pulls the latest version of index.html from
GitHub repository into his/her local machine, then he/she will
receive a merge conflict message due to line 10.
The screen shot below shows the pattern in which the index.html
will appear to programmer x .
The line of code written within the head and === are changes made
by programmer x and the lines of code written within === and
>>>>a99… are the changes made by programmer y .
For better understanding, let’s create a merge conflict scenario by
following the steps below.
Let’s commit the changes (do not push the changes to GitHub
repository ).
Step 2: Open the GitHub repository page and update index.html
file at the same line containing <h3> tag as shown in the screen shot
below and commit the changes.
Step 3: In our local machine, pull the index.html file from GitHub
repository using git pull command.
Since changes were made to the same file and to the same line
containing <h3> tag, Git will throws a merge conflict message as
shown in the screen shot below.
Step 4: Let’s refresh index.html file in our local machine
Step 5: In order to resolve this issue, delete all the merge conflict
messages from index.html file and update the line containing <h3>
tag which suits the best.
Step 6: Now let’s push the updated index.html file into the GitHub
repository .
NOTE: Shortcut of git add + git commit is:
git commit –am “ commit message ”
Chapter 7 : Git Branching
Git branching allows a programmer to work on different versions of
the same file without disturbing the main master source code of the
project. For example: Let us consider in branch master , you have
the original main source code of the project. Suppose you want to
work on a new feature but you do not wish to disturb the original
functionality of the main project. So for this reason another branch
(suppose test) is created. In branch test you create the new feature
and once it gets approved by the clients that new feature is merged
with the main project source code present in branch master .
Now type git branch command to view all the available branches
Step 4: Refresh index.html file and you will notice that the styling
information which we added to <h1> tag in branch test is not
present as shown in the screen shot below.
I was satisfied with the changes I made to index.html in branch test
and would like to include those changes in branch master .
In order to merge the changes from branch test to branch master
git merge branch_name command is used. git merge branch_name
command merges the specified branch (branch test ) into the
currently active branch (branch master ).
Refresh index.html file and you will notice that the additional lines of
code from branch test are incorporated into index.html of branch
master .
Other important Git commands
git rm filename
git status
git log