GIT AND GITHUB LESSON
GIT AND GITHUB LESSON
It was created
Is a version
by Linus
control system.
Torvalds in 2005
Introduction to Git
• Why Git?
• Over 70% of developers use Git!
• Developers can work together from anywhere in the world.
• Developers can see the full history of the project.
• Developers can revert to earlier versions of a project.
What is GitHub
GitHub is the largest host of source code in the world, and has
been owned by Microsoft since 2018.
This lesson focuses
on using Git with
Github
• Install git
• You can download Git:
First things first https://www.git-scm.com/
First things first
• This action is very important for version control systems because each git commit uses
this information.
• Type git config --global user.name “nanakannan” then enter
• Type git config --global user.email “drnanakannan@gmail.com” enter
Creating git folder
• Once you have navigated to the correct folder, you can initialize Git on that folder:
• Git init
• This will initialise the empty fit repository on local machine
• Git now knows that it should watch the folder you initiated it on.
• Git create a hidden folder to keep track of changes
Git new files
files • 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
Git staging As you are working, you may be adding, editing and removing
files. But whenever you hit a milestone or finish a part of the
environme work, you should add the files to a Staging Environment.
You can add more than one file by using the following command:
Git is a point in the project you can go back to if you find a bug, or
want to make a change.
• Without Git:
• Make copies of all the relevant files to avoid impacting the live version
• Start working with the design and find that code depend on code in other files, that also need to be changed!
• Make copies of the dependant files as well. Making sure that every file dependency references the correct file name
• EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!
• Save all your files, making a note of the names of the copies you were working on
• Work on the unrelated error and update the code to fix it
• Go back to the design, and finish the work there
• Copy the code or rename the files, so the updated design is on the live version
• (2 weeks later, you realize that the unrelated error was not fixed in the new design version because you copied the files before
the fix)
Working with git branches
• With Git:
• With a new branch called new-design, edit the code directly without impacting the main branch
• EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!
• Create a new branch from the main project called small-error-fix
• Fix the unrelated error and merge the small-error-fix branch with the main branch
• You go back to the new-design branch, and finish the work there
• Merge the new-design branch with main (getting alerted to the small error fix that you were missing)
Working with git branches
• 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.
• We are working in our local repository, and we do not want to disturb or possibly wreck
the main project.
• checkout is the command used to check out a branch. Moving us from the current branch,
to the one specified at the end of the command:
• Git checkout new-file-name
• Now we have moved our current workspace from the master branch, to the new branch
• We have made changes to a file and added a new file in the working directory (same
directory as the main branch).
• There are changes to our new-file-name, but the file is not staged for commit
• The changed file is not tracked
• So we need to add both files to the Staging Environment for this branch:
New git branch
• We are happy with our changes. So we will commit them to the branch:
• Git commit -m “Some changes has been made to this file”
• Now we have a new branch, that is different from the master branch.
• Note: Using the -b option on checkout will create a new branch, and move to it, if it does
not exist
Switching between branches
• Now let's see just how quick and easy it is to work with different branches, and how well it
works.
• We are currently on the branch new-file-name. We made some changes to this branch, so
let's list the files in the current directory:
• Type ls
• We can see the new file, and if we open the file, we can see the changes. All is as it should
be.
Now, let's see what happens when we change branch to master
Switching The new file is not a part of this branch. List the files in the
between current directory again:
branches Type ls
See how easy it is to work with branches? And how this allows
you to work on different things?
Now imagine that we are not yet done with new-file-
name, but we need to fix an error on master.
Now we have created a new branch from master, and changed to it.
We can safely fix the error without disturbing the other branches.
Emergency
branch Let's fix our imaginary error:
We have made changes in this file, and we need to get those changes
to the master branch.
Merge Since the emergency-fix branch came directly from master, and no other
changes had been made to master while we were working, Git sees this as
a continuation of master. So it can "Fast-forward", just pointing both
Branches master and emergency-fix to the same commit.
As master and emergency-fix are essentially the same now, we can delete
emergency-fix, as it is no longer needed:
Git status
Now you have a better understanding of how branches and merging works.
Time to start working with a remote repository!