Introduction to Version Control with SVN & Git CSC/ECE 517, Fall 2012 Titus Barik...

Preview:

Citation preview

Introduction to Version Control with SVN & Git

CSC/ECE 517, Fall 2012Titus Barik (tbarik@ncsu.edu)

& Ed Gehringer, with help fromGaurav Tungatkar (gstungat@ncsu.edu

)

Local version control: RCS

• Keep many copies of files

• Error prone• RCS stores deltas

Version 3

Version 2

Version 1

Filecheck out

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

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?

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!

File Sharing Issues, 2

Image: Version Control with Subversion

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

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.

Approach 2: Copy-Modify-Merge

Image: Version Control with Subversion

Sounds chaotic, but in practice, runs extremely smoothly.

Question: When is locking necessary?

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

• 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

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)

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.

Creating a Branch

Image: Version Control with Subversion

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

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?

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

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)

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

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

3 States of a File in Git

• Modified• Staged • Committed

working directory

staging areagit directory(repository)

check out the project

stage files

commit

File Status Lifecycle

unmodified modified staged

edit the file

stage the file

untracked

add the file

remove the file

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)

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

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

Pulling, pushing to remote

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

• git push origin master

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

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?

Recommended