Sei sulla pagina 1di 15

Version Control

What is Version Control


A version control system is a system which allows for the management of a
code base.

What kinds of files can we use it with?


• Source code
• Documentation
• Configuration files
• Short stories
• Binary files (music and pictures)

What should we use it for?


• Text files
• Projects that have lots of revisions (changes)
• Collaborating
Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 2
More Uses of Version Control
• Version control is not just useful for collaborative working, essential for
quality source code development

• Often want to undo changes to a file


o start work, realize it's the wrong approach, want to get back to starting point
o like "undo" in an editor…
o keep the whole history of every file and a changelog

• Also want to be able to see who changed what, when


o The best way to find out how something works is often to ask the person who
wrote it

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 3


Version Control Tools Example
Lots of different choices available:

• CVS
• SVN
• Perforce
• Git
• Mercurial (Hg)
• Bazaar
• And more!

Most tools follow a repository model (though differ in how the repositories work)

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 4


Centralized VCS
• One central repository

• Clients access the server by means of


check-in/check-outs

• Examples include CVS, Subversion,


Visual Source Safe.

• Advantages: Easier to maintain a


single server.

• Disadvantages: Need to solve issues


with group members making different
changes on the same files

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 5


Distributed VCS
• Everyone has a working repository

• Code is shared between clients by


push/pulls

• Advantages: Many operations cheaper.


No single point of failure. Faster.
Connectionless

• Disadvantages: A bit more


complicated. Still need to resolve
issues, but it's not an argument
against DVCS

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 6


About GIT
• Created by Linus Torvalds, creator of
Linux, in 2005
o Came out of Linux development community
o Designed to do version control on Linux
kernel

• Goals of Git:
o Speed
o Support for non-linear development
(thousands of parallel branches)
o Fully distributed
o Able to handle large projects efficiently – (A
"git" is a cranky old man. Linus

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 7


GIT Snapshot
• Centralized VCS like Subversion track
version data on each individual file.

• Git keeps "snapshots" of the entire


state of the project.
o Each checkin version of the overall code has
a copy of each file in it.
o Some files change on a given checkin, some
do not.
o More redundancy, but faster.

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 8


Local GIT Areas
• In your local copy on git, files can be:

o In your local repo


• (committed)

o Checked out and modified, but not yet


committed
• (working copy)

o Or, in-between, in a "staging" area


• Staged files are ready to be committed.
• A commit saves a snapshot of all staged
state.

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 9


Basic GIT Workflow
• Modify files in your working directory.

• Stage files, adding snapshots of them


to your staging area.

• Commit, which takes the files in the


staging area and stores that snapshot
permanently to your Git directory.

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 10


GIT Advantages & Disadvantages
• Resilience • Definite learning curve, especially for
o No one repository has more data than any those used to centralized systems
other o Can sometimes seem overwhelming to learn
• Speed • Conceptual difference
o Very fast operations compared to other • Huge amount of commends
VCS (I’m looking at you CVS and
Subversion)
• Space
o Compression can be done across repository
not just per file
o Minimizes local size as well as push/pull
data transfers
• Simplicity
o Object model is very simple
• Large user base with robust tools

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 11


GIT Commands

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 12


Add & Commit a File
• The first time we ask a file to be tracked, and every time before we commit a
file, we must add it to the staging area:
• git add Hello.java Goodbye.java
• Takes a snapshot of these files, adds them to the staging area.
• In older VCS, "add" means "start tracking this file." In Git, "add" means "add to staging area" so it will
be part of the next commit.

• To move staged changes into the repo, we commit:


• git commit –m "Fixing bug #22"

• To undo changes on a file before you have committed it:


• git reset HEAD -- filename (unstages the file)
• git checkout -- filename (undoes your changes)
• All these commands are acting on your local version of repo.

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 13


Viewing/Undoing Changes
• To view status of files in working directory and staging area:
• git status or git status –s (short version)

• To see what is modified but unstaged:


• git diff

• To see a list of staged changes:


• git diff --cached

• To see a log of all changes in your local repo:


• git log or git log --oneline (shorter version)
1677b2d Edited first line of readme
258efa7 Added line to readme
0e52da7 Initial commit
‒ git log -5 (to show only the 5 most recent updates), etc.
Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 14
Workflow Example
$ emacs rea.txt
$ git status
no changes added to commit
(use "git add" and/or "git commit -a")
$ git status -s
M rea.txt
$ git diff
diff --git a/rea.txt b/rea.txt
$ git add rea.txt
$ git status
# modified: rea.txt
$ git diff --cached
diff --git a/rea.txt b/rea.txt
$ git commit -m "Created new text file"

Presentation Title | Author | Date © 2017 Capgemini. All rights reserved. 15

Potrebbero piacerti anche