26
Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik ([email protected] ) & Ed Gehringer, with help from Gaurav Tungatkar ( [email protected] )

Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik ([email protected])[email protected] & Ed Gehringer, with help from Gaurav

Embed Size (px)

Citation preview

Page 1: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Introduction to Version Control with SVN & Git

CSC/ECE 517, Fall 2012Titus Barik ([email protected])

& Ed Gehringer, with help fromGaurav Tungatkar ([email protected]

)

Page 2: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Local version control: RCS

• Keep many copies of files

• Error prone• RCS stores deltas

Version 3

Version 2

Version 1

Filecheck out

Page 3: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Centralized Version Control• If you need to work with other

programmers …

Version 3

Version 2

Version 1

File check out

File check out

Computer B

Computer A

Page 4: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

File Server vs. Version-Control Server

At first glance, the client-server architecture of a version-control system looks much like a typical file server.

So why do we need version control?

Page 5: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

File Sharing Issues, 1

• Assume your project has 15 developers: – Each developer wants to make 1

change and must communicate this change to all other developers.

– What are the total number of interactions that must occur?

• C(15, 2) = N(N – 1)/2 = 15(14)/2 = 105. That’s a lot of information to track!

Page 6: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

File Sharing Issues, 2

Image: Version Control with Subversion

The problem is that users are stepping on each other’s feet!

Page 7: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Approach 1: Lock, Modify, Unlock

Image: Version Control with Subversion

1. Locking may cause administrative problems.

2. Locking may cause unnecessary serialization.

3. Locking may create a false sense of security.

Page 8: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Approach 2: Copy-Modify-Merge

Image: Version Control with Subversion

Sounds chaotic, but in practice, runs extremely smoothly.

Question: When is locking necessary?

Page 9: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Subversion (SVN)

• Open source, Apache License.• Available on all major operating

systems.• Improvements such as “cheap”

copying and branching, remote options (https), over legacy CVS.

• However, CVS has more mature tools, and SVN has it own flaws (DVCS).

Page 10: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

• Answer these questions

• Give one advantage of using a version-control server for source-code management over using a fileserver.

• Suppose a project has 15 developers and no shared repository. Can you come up with a better way of communicating changes than N(N-1)/2 interactions?

• Explain how locking can cause administrative problems.

• Explain how locking can create a false sense of security.

• With copy-modify-merge, when is locking necessary?

Exercise 1

Page 11: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Branches and Tags

Trunk: Location where main development occurs.

Branches: Location used to isolate changes to another development line (e.g., experimental features).

Tags: Snapshot of the content (e.g., RTM, service packs, EOL).Image: http://en.wikipedia.org/wiki/Subversion_(software)

Page 12: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Populated Repository

Image: Version Control with Subversion

A Subversion repository layout. The folder names are just a convention, and have no special meaning to the repository.

Page 13: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Creating a Branch

Image: Version Control with Subversion

In Subversion, the underlying mechanism of a branch is implemented by performing a simple directory copy.

Page 14: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Exercise 2

• Answer these questions about branches.o Suppose, in fixing a bug, you modify three

lines of code in two source files. Should you create a new branch? Why or why not?

o Which would probably be more common, branches or tags?

o What are some of the risks of copying files in a repository? How do version-control systems minimize this risk?

Page 15: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Distributed Version Control

• Clients don’t checkout individual files;

• they mirror therepository.

• What’s theadvantage?

Version 3

Version 2

Version 1

Version 3

Version 2

Version 1

Version 3

Version 2

Version 1

File File

Computer A Computer B

Page 16: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Git

• Came out of the Linux project, in 2005.• Simple design • Strong support for non-linear

development (thousands of parallel branches)

• Fully distributed • Able to handle large projects like the

Linux kernel efficiently (speed and data size)

Page 17: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Integrity & Checksums

• Everything checksummed with an SHA-1 hash– 40-character string – composed of hex characters – calculated based on the contents of a file or

directory structure in Git

• Example– 24b9da6552252987aa493b52f8696cd6d3b00

373

• Git knows everything by hash, not filename

Page 18: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Snapshots, not Diffs

• See http://progit.org/book/ch1-3.html

• Every time you commit, Git takes a snapshot of your files.

• Files that have not changed are not copied.

Almost all ops are local• browse history• commit

Page 19: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

3 States of a File in Git

• Modified• Staged • Committed

working directory

staging areagit directory(repository)

check out the project

stage files

commit

Page 20: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

File Status Lifecycle

unmodified modified staged

edit the file

stage the file

untracked

add the file

remove the file

Page 21: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Checking Status

• To check the status of your files:$ git status # On branch master nothing to commit (working directory clean)

• Creating new files$ vim README $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track)

Page 22: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Checking status, cont.

• Begin to track the file:$ git add README

• The file is now tracked:$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: README #

• For more info:http://progit.org/book/ch2-2.html

Page 23: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Remotes

• On a project, you may be working with several remote directories.

• “Origin” is the server you cloned your repository from$ git clone git://github.com/schacon/ticgit.git Initialized empty Git repository in /private/tmp/ticgit/.git/ remote: Counting objects: 595, done. remote: Compressing objects: 100% (269/269), done. remote: Total 595 (delta 255), reused 589 (delta 253) Receiving objects: 100% (595/595), 73.31 KiB, done. Resolving deltas: 100% (255/255), done. $ cd ticgit $ git remote origin

• http://progit.org/book/ch2-5.html

Page 24: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Pulling, pushing to remote

• $ git fetch [remote-name]• E.g., git fetch origin

• git push origin master

Page 25: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Common Workflow using Git

• Centralized workflow …• http://progit.org/book/ch5-1.html

• Integration-manager workflow …• Common use cases:

http://progit.org/book/ch5-2.html

Page 26: Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik (tbarik@ncsu.edu)tbarik@ncsu.edu & Ed Gehringer, with help from Gaurav

Exercise 3

• Answer these question about DVCSs.o What is an advantage of referring to

files and branches by hash, rather than by filename?

o Can you think of one disadvantage of a DVCS compared to a centralized VCS?