0% found this document useful (0 votes)
17 views104 pages

Git and Github

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 104

Sunitha C S

Joint Director
STDC, CDAC, Kochi
Project Teams- Past Experiences
When you were an engineering student you are probably used
to programming courses that focus on solving relatively small
problems, often (but not always) in isolation. Perform a binary
search, traverse a graph, implement stacks and queues, etc..
• There will only be one user at a time.
• The code will only be seen by the group members and trashed (be
forgotten) after the project is done.
• The project will run only when the instructor has to evaluate it or
for testing, probably on the machine of the team member.
• Continuous integration means “continuously integrate new code,
compile and test until things work”.
• Testing means “make sure it doesn’t throw a logical or
segmentation fault when you do that thing that caused a logical or
segmentation fault last time”.
Developing an application in a team
Team Work
• If you work on a project you’ll likely be part of a team.
• Different modules of the project may be assigned to each
member. Task with the members may be different also.
• As a group effort, the complete development can be done.
Issues developers face when working in a team

• Consider multiple developers work on the same project.


Sharing the same stack might yield unexpected results.
• Imagine Alice and Bob start working on the same state, the
one on top of the stack. Now imagine they both change the
same set of files but in different ways.
• But it be nice if every developer could work on a separate stack
and, only once a feature is complete or a bug is fixed, “merge”
that stack with the main one. This technique is called
“branching”. Because of its popularity and relatively ease of
use we’ll take a look at the VCS git.
Introduction to Code Versioning System (CVS)
• A version control system records the changes made to our files over
time, in a special database called Repository.
• The name says it all: It is a way of keeping versions of files stored in
a repository. In our case these files happen to be text representing
the source code of a program, nonetheless a VCS (Version Control
System) can be used for all sorts of files.
• That is, imagine that a set of files and directory represents a state
in time. Without a VCS you end up overwriting those files and,
therefore, rewriting the state. With a VCS, you keep the states as a
stack. At any point in time you can retrieve an older state or add a
new one on top of the stack.
Why a Version Control System
• Real life projects generally have multiple developers working
in parallel. So a version control system like Git is needed to
ensure there are no code conflicts between the developers.
• Additionally, the requirements in such projects change often.
So a version control system allows developers to revert and go
back to an older version of the code.
History of code versioning system
• Different tools available for versioning
SDLC – Software Development Life
Cycle
• Software development workflow
Version Control System : Benefits
Version Control System : Benefits
In short, a version control system like Git makes it easy to:
• Keep track of code history
• Collaborate on code as a team
• See who made which changes
• Deploy code to staging or production
Version Control System : Tools
Introduction to git
• Git is an Open Source Distributed Version
Control System.
Git History
What is Git?
Features of Git
Who Uses Git?
Version Control Systems : Details
Version Control Systems : Details
• Centralized VCS
• Distributed VCS
Centralized Version Control System
Distributed Version Control System
Git workflow
There are three main components of a Git project:
• The repository, or repo, is the “container” that tracks the changes to your
project files. It holds all of the commits — a snapshot of all your files at a point
in time — that have been made. You can access the commit history with the
Git log.
• The working tree, or working directory, consists of files that you are currently
working on. You can think of a working tree as a file system where you can
view and modify files.
• The index, or staging area, is where commits are prepared. The index
compares the files in the working tree to the files in the repo. When you make
a change in the working tree, the index marks the file as modified before it is
committed.
Git workflow
Git workflow
You can think of this as your basic Git workflow:
• Modify your files in the working tree.
• Stage the changes you want to include in the next commit.
• Commit your changes. (Committing will take the files from the
index and store them as a snapshot in the repository.)
Three states of Git files
As you can probably guess from the Git workflow, files can be in one
of three states:
• Modified
• Staged
• Committed
• When you modify a file, the change will only be found in the
working tree. You must then stage the changes if you want to
include them in your next commit. Once you finish staging all files,
you can commit them and add a message describing what you
changed. The modified files will then be safely stored in the repo.
Three states of Git files
Git : Life Cycle
Git vs GitHub
Lab 1 : Install Git and verify
Lab 2 : Configure Git username and email
Lab 1 and 2
• Verify git by checking git version
git --version
• Configure user and email
git config -- global user.name “Sunitha”
git config -- global user.email “[email protected]
Remote repositories and local repositories
There are two types of Git repositories: remote and local.
• A remote repository is hosted on a remote, or off-site, server that
is shared among multiple team members.
• A local repository is hosted on a local machine for an individual
user.
Remote repositories and local repositories
Ways to create a repository
• There are two ways to create a local repository on your machine: you
can create a new repository from scratch using a file folder on your
computer, or you can clone an existing repository.
• Git init - You can create a new repo from scratch using the git
init command. It can be used to introduce Git into an existing,
unversioned project so that you can start tracking changes.
• Git clone - You can copy a remote repository onto your local machine
using the git clone command. By default, git clone will automatically
set up a local master branch that tracks the remote master branch it
was cloned from.
Recording changes
• Git does not automatically save every change you make. You must tell Git which
changes you want to be recorded by staging those changes. After staging, you
can then commit the changes so that they are recorded in the repo.
• Making changes - The working tree is where you make changes. There you can
edit files, add new files, and remove files that are no longer needed. Once a file
has been changed in the working tree, it is noted as modified in the index (e.g.,
the staging area where new commits are prepared) where it sits between the
repository and the working tree.
• Changes made in the working tree are not saved directly to the repository. All
changes are first staged in the index and then saved in the repo. Only the files
in the index are committed to the repo.
Git commit
• The git commit command lets you record file changes in the
repository’s Git history.
• Every change you commit will be viewable in the respective file or
directory in chronological order.
Git commit
• A 40-character checksum hash uniquely identifies each commit.
You can use the checksum hash to retrieve the status or changes
that were made on the given commit in your repository.
• When committing your changes, you are required to enter a
commit message. The commit message should accurately describe
the changes you’re making.
• Make commit messages easy to understand for all your team
members.
Lab 3 : Create a Git Repository
Undoing changes
• One of the most valuable features of Git is the ability to undo
mistakes. When you make a new commit, Git stores a snapshot
of your project so that you can go back to an earlier version
when you need to.
• There are two ways to undo changes: git revert and git reset.
Git revert
• You can use the git revert command to safely undo a commit
that has already been pushed.
• While you can also delete a previous commit from the history
using git reset or git rebase -i, it is generally not a good idea
because it causes the remote repository to become
desynchronized with the local repositories of other members.

git revert is the safest method of undoing changes.


Git reset
• You can discard commits that you no longer need using the git
reset command. You can specify the scope for the reset
command by going into reset mode.

Use git reset to remove unnecessary commits.


Lab 4 : Undoing changes
• Create a user registration and login features in a java project
• Commit the project
• Add a function to reset the password
• Commit the changes
• Undo the last commit
Git commands

• git branch feature1


Git commands contd..

• git checkout feature1


Lab 5 : Tracking File Changes
Git Buzzwords : master
Git Buzzwords : branch
Git buzzword : head
remote
origin
clone
push
pull
fetch
Upstream and Downstream
Each developer will work in their local repository but
eventually, they will push the code into a remote
repository. Once the code is in the remote repository,
other developers can see and modify that code.
Syncing repositories
• Remote repositories allow us to share our changes with other
members of the team. They can be on a private server, on a
different computer than yours, or hosted using a service like
Backlog. Wherever yours is hosted, you’ll need to be able to
sync your local repository with the remote repository
frequently. You’ll do this using three commands: git push, git
pull, and git merge.
Git push
• In order to start sharing changes with others, you have to push
them to a remote repository using the “push” command. This
will cause the remote repository to update and synchronize
with your local repository.
Git pull
• Whenever somebody pushes their changes to the shared
remote repository, your local repository becomes out of date.
To re-synchronize your local repository with the newly updated
remote repository, simply run the git pull operation.
• When the pull is executed, the latest revision history will
download from the remote repository and import to your local
repository.
Git pull
Git merge
• Your push to the remote repository will be rejected if your local
repository is out of date, possibly because there are some
updates on the remote repository that you do not have locally
yet.

You are unable to push to the remote repository if your You must merge the latest changes before pushing.
local repo is out of date.
Git commands contd..
Git commands contd..
• git remote add origin url/to server/repo
• git clone /path/to/repo
• git clone url/to/repo
• git push origin master
• git push origin branchName
• git pull
• git merge branchName
Lab 6 : Create Repository in GitHub
Lab 7 : Clone a Git Repository
GitHub vs GitLab vs Bitbucket
Lab 8 : Centralized Git Workflow
Lab 9 : Collaboration between Local and Remote
Repositories
Lab 10 : Merging file changes in Git
Updating Eclipse

• If the Git tooling is not available, you can install it via the Eclipse
installation manager. Select the Help --> Install new
Software…​ menu entry. Enter one of the following update site
URLs:
• http://download.eclipse.org/egit/updates # Use this update site
to get the latest release
• http://download.eclipse.org/egit/updates-nightly/ # use this
update site to get the night build
Configuration for Git usage via the Eclipse IDE

• In your Eclipse IDE, select


the Window → Preferences → Version
Control (Team) → Git
→ Configuration entry. Configure your full
name and email in the user settings. As the
Eclipse IDE uses the same settings as the Git
command line, this might already be done.
• If these keys are not available press the Add
Entry…​ button and add them.
Create a new Git repository via Eclipse

Select menu File → new → other and select


• Git Repository
Put project under Git version control

• Add java files as needed


• To put your new project under version control with Git, right-
click on your project, select Team → Share Project. If another
version control system is installed you have to select that you
want to use Git as the version control system.
• Select your existing Git repository from the drop-down list.
Using the Git Staging view for the initial commit

• Window → other → git staging


area
• In this view use the + button to
stage
all files.
• After adding to staging, write a
meaningful commit message and press
the Commit button.
Open an older version with the current
version of a file via the History view
Push to remote
Configuring remote repository
Git Push to remote
Import project from GitHub

• For this, select File → Import → Git →


Projects from Git → Clone URI
Questions????

You might also like