0% found this document useful (0 votes)
10 views

GIT AND GITHUB LESSON

This document provides an introduction to Git and GitHub, explaining their functionalities, including version control, project management, and collaboration among developers. It covers essential commands for initializing repositories, staging and committing changes, and managing branches to facilitate parallel development. The lesson emphasizes the importance of Git in modern software development and guides users through practical steps to effectively use Git with GitHub.

Uploaded by

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

GIT AND GITHUB LESSON

This document provides an introduction to Git and GitHub, explaining their functionalities, including version control, project management, and collaboration among developers. It covers essential commands for initializing repositories, staging and committing changes, and managing branches to facilitate parallel development. The lesson emphasizes the importance of Git in modern software development and guides users through practical steps to effectively use Git with GitHub.

Uploaded by

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

GIT AND GITHUB LESSON

NANA KOFI ANNAN, PhD


Introduction to Git

It was created
Is a version
by Linus
control system.
Torvalds in 2005
Introduction to Git

• What is it use for?


• Tracking code changes
• Tracking who made changes
• Coding collaboration
Introduction to Git

• 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
Introduction to Git

• Overview of 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!
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

Git is not the same as GitHub.

GitHub makes tools that use Git.

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

Now that git is The first thing we


installed, let’s must do is to
open the check if git is
command shell properly installed:
First things first

Type git --version

This will display the version of your git.


First things first – Configure git

• 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

You are going to create a new folder for your project


• mkdir foldername
• cd foldername

*mkdir makes a new directory.

*cd changes the current working directory.


Initialize git

• 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

• Adding new files


• You just created your first local Git repo. But it is empty.
• So let's add some files by saving or move it to the folder you just created.
• Let's go back to the terminal and list the files in our current working directory
• Type ls for git to display the files in the directory on the screen.
• 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


Git new states:

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

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.
Git staging environment
One of the core functions of Git is the concepts of the Staging
Environment, and the Commit.

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.

nt Staged files are files that are ready to be committed to the


repository you are working on. You will learn more about
commit shortly.

For now, we are done working on file. So we can add it to the


Staging Environment:
Git staging Type git add filename

environme The file should be staged. Let’s check the status

• Type the following:


nt • Git status

Now the file has been added to the staging environment


Git add more than one file

You can add more than one file by using the following command:

• Git add --all


• Using --all instead of individual filenames will stage all changes (new, modified, and
deleted) files.
• Note: The shorthand command for git add --all is git add -A

The next thing after staging is to commit.


Since we have finished our work, we are ready 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

Git is a point in the project you can go back to if you find a bug, or
want to make a change.

commit 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.
Git commit - example

We commit by using the following command:


• Git commit –m “First release of app”

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 release of app!"
Git commit • To view the history of commits for a repository, you can use
the log command:
log • Git log
If you are having trouble remembering
commands or options for commands, you can
use Git help.
There are a couple of different ways you can
use the help command in command line:

Git help git command -help - See all the available


options for the specific command

git help --all - See all possible commands


Git Branch
In Git, a branch is a new/separate
version of the main repository.
Working
with git Let's say you have a large project, and
branches you need to update the design on it.

How would that work without and


with Git:
Working with git branches

• 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.

• Branching in Git is very lightweight and fast!


New git branch

• Let add some new features to our file.

• We are working in our local repository, and we do not want to disturb or possibly wreck
the main project.

• So we create a new branch:


New git branch

• Type git branch new-file-name


• Now we created a new branch called “new-file-name"

• Let's confirm that we have created a new branch:


• Type git branch
• We can see the new branch with the name “new-file-name", but the * beside master
specifies that we are currently on that branch.
New git branch

• 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

• You can now edit/update your file.


New git branch

• We have made changes to a file and added a new file in the working directory (same
directory as the main branch).

• Now check the status of the current branch:


• Git status
New git branch

• So let's go through what happens here:

• 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

• Type git add --all


• Using --all instead of individual filenames will Stage all changed (new, modified, and
deleted) files.

• Check the status of the branch:


• Git status
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

Type git checkout master

Switching The new file is not a part of this branch. List the files in the
between current directory again:

branches Type ls

The new-file-name is no longer there! And if we open the file, we


can see the code reverted to what it was before the alteration.

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.

I don't want to mess with master directly, and I do


not want to mess with new-file-name, since it is not
Emergency done yet.

branch So we create a new branch to deal with the


emergency:

Type git checkout -b emergency - fix


So we create a new branch to deal with the emergency:

Type git checkout -b emergency – fix

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.

Check the status:


Type git status

stage the file, and commit:

Emergency Git add new-file-name


branch
Git commit -m “updated file with
emergency fix”
Now we have a fix ready for master, and
we need to merge the two branches.
Git Branch Merge
We have the emergency fix ready,
and so let's merge the master and
emergency-fix branches.
First, we need to change to the
Merge master branch:
Branches
Git checkout master
Now we merge the current branch (master) with emergency-fix:

Git merge emergency - fix

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 branch -d emergency -fix


Merge
Conflict
As master and emergency-fix
are essentially the same now,
we can delete emergency-fix, as
it is no longer needed:
Merge
Conflict

Git checkout new-file-name


Now, we are done with our work
here and can stage and commit for
this branch:

Merge Git add --all


Conflict
Git commit -m “added some
changes”
We see that the new file has been changed in
both branches. Now we are ready to merge
the files into master. But what will happen to
the changes we recently made in master?

Merge Git checkout master


Conflict
Git merge new-file-name
The merge failed, as there is conflict between the
versions for index.html. Let us check the status:

Git status

Merge This confirms there is a conflict in index.html, but the


image files are ready and staged to be committed.
Conflict
So we need to fix that conflict. Open the file in our
editor:

We can see the differences between the versions and


edit it like we want:
Now we can stage the new-file-name
and check the status:

Git add new-file-name


Merge
Conflict Git status

The conflict has been fixed, and we can


use commit to conclude the merge:
Merge Conflict

Type git commit -m “merge new-file-name after fixing conflicts”

And delete the new-file-name branch:

Git branch -d new-file-name

Now you have a better understanding of how branches and merging works.
Time to start working with a remote repository!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy