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

Git

Git is a free, distributed version control system that allows programmers to keep track of code changes via "snapshots" (commits) and allows for branches to exist alongside each other. Git uses a local repository and remote repositories, such as GitHub, can be used for collaboration. Version control records changes to files over time so that specific versions can be recalled later.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Git

Git is a free, distributed version control system that allows programmers to keep track of code changes via "snapshots" (commits) and allows for branches to exist alongside each other. Git uses a local repository and remote repositories, such as GitHub, can be used for collaboration. Version control records changes to files over time so that specific versions can be recalled later.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

• Git is a free, distributed version control system

which allows programmers to keep track of


code changes, via "snapshots" (commits), in
its current state.
• Git also allows for users to create new
"branches" of the code, which allows different
versions of the code to live alongside each
other
• Git local repo
• Github Remote repo
What is version control ?

• Version control is a system that records


changes to a file or set of files over time so
that you can recall specific versions later
Local Version Control Systems

• Many people’s version-control method of


choice is to copy files into another directory
• This approach is very common because it is so
simple, but it is also incredibly error prone.
Centralized Version Control Systems

• The next major issue that people encounter is


that they need to collaborate with developers
on other systems.
• To deal with this problem, Centralized Version
Control Systems (CVCSs) were developed.
• These systems (such as CVS, Subversion, and
Perforce) have a single server that contains all
the versioned files, and a number of clients that
check out files from that central place. 
Distributed Version Control Systems

• In a DVCS (such as Git, Mercurial, Bazaar or


Darcs), clients don’t just check out the latest
snapshot of the files; rather, they fully mirror
the repository, including its full history.
• Thus, if any server dies, and these systems
were collaborating via that server, any of the
client repositories can be copied back up to
the server to restore it. Every clone is really a
full backup of all the data.
The Three States
GIT
• Modified means that you have changed the
file but have not committed it to your
database yet.
• Staged means that you have marked a
modified file in its current version to go into
your next commit snapshot.
• Committed means that the data is safely
stored in your local database.
• This leads us to the three main sections of a
Git project:
• the working tree,
• the staging area
• the Git directory.
Git workflow

• You modify files in your working tree.


• You selectively stage just those changes you
want to be part of your next commit, which
adds only those changes to the staging area.
• You do a commit, which takes the files as they
are in the staging area and stores that
snapshot permanently to your Git directory.
Installing Git

• Installing on Linux
• If you want to install the basic Git tools on Linux
via a binary installer, you can generally do so
through the package management tool that comes
with your distribution.
• $ sudo dnf install git-all
• $ sudo apt install git-all
• Git website, at
•  https://git-scm.com/download/linux.
 Creating a repository

• To begin using Git, we have first to create a


repository, also known as “repo”.
• For that, in the directory where we want to
have the repository.
Creating the history: commits

• Git constructs the history of the repository


with commits. 
• A commit is a full snapshot of the repository,
that is saved in the database. 
• When doing a commit, we have to choose which files are
going to be committed; not all the repository has to be
committed necessarily.
• This process is called staging, where files are added to the
index.
• The Git index is where the data that is going to be saved in
the commit is stored temporarily, until the commit is done.
• Adding this file, the status of the repository has changed,
since a new file has been created in the working directory.
• We can check for the status of the repository with
the status option
• What Git is saying is “you have a new file in the
repository directory, but this file is not yet selected to
be committed“.
• If we want to include this file the commit, remember
that it has to be added to the index. This is made
with the add command, as Git suggests in the output
of status 
• $ git status
• $ git commit
• Now, the default text editor will be shown, where we have to type the
commit message, and then save. 
• If we leave the message empty, the commit will be aborted.
• Additionally, we can use the shorthand version with -m flag, specifying the
commit message inline
echo 'Second commit' > README.txt
git add README.txt
echo ' third commit' > README.txt
git staus
Viewing the history
Creating a branch with Git
• To start working in that branch, we have
to checkout it

$ git checkout second-branch


• Now, the commits will only exist in second-branch.
Why? Because the HEAD now is pointing to second-
branch, so, the history created from now will have an
independent path from master.
echo 'The changes made in this branch...' >> README.txt
git add README.txt
git commit -m 'Start changes in second-branch'
echo '... Only exist in this branch' >> README.txt
git add README.txt
git commit -m 'End changes in second-branch'
•  After creating the history of second-branch,
we have placed the HEAD pointing to master
Combining histories: merging branches

• In the previous subsection, we have seen how we can


create different paths for our repository history. Now,
we are going to see how to combine them, what for
Git is calling merging.
• Let’s suppose that, after the changes made in second-branch,
is ready to return to master. For that, we have to place
the HEAD in the destination branch (master), and specify the
branch that is going to be merged to this destination branch
(second-branch), with merge command:
 Remote repositories

• Git, as we have seen in the introduction, is a distributed VCS.


This means that, apart from the local, we can have a copy of
the repository hosted in a remote server that, apart from
making public the source code of the project, is used for
collaborative development.

You might also like