.at • Daniel Knittl-Frank
Version Control Workshop
Linux User Group FH Hagenberg
Overview
Version control in general
Centralized version control systems
Distributed version control systems
Hands-on (Git)
Reasons to Use Version Control
Have a record of what you did, when and why
Snapshots of different development states
Compare different versions of files
Easily share work with others
Integrate changes from others (merging )
Mark finished product versions (tags )
Try out new ideas (branches )
Best practice
When Not?
Backups (use a backup solution!)
Glossary
Repository
Stores complete history, branches, tags
Working Copy
Your “playground”, actual source code
Commit / Revision
A single version
Branch
A separate line of development
Centralized Version Control
Single repository on a server
Each user has a working copy
Only privileged users can commit
Distributed Version Control
Every user has a full copy of the repository
Repositories can be synchronized (push/pull)
Off-line access
Git,
Mercurial,
Monotone,
Fossil,
Bazaar
Hands-On
Git, the information manager from hell
Git
Started in 2005 by Linus Torvalds
Used for Linux kernel development
Highly distributed
Cryptographic integrity
Install & Setup
Install
Set user information
Windows users might want to set core.autocrlf
(Documentation )
# apt-get install git gitk
$ git config --global user.name 'Daniel Knittl-Frank'
$ git config --global user.email 'S1010454…@students.fh-hagenberg.at'
$ git config --global core.autocrlf false
Creating repositories
$ cd path/to/project
$ git init
$ git init new_dir
$ cd new_dir
Adding files
$ >foo echo 'bar'
$ git add foo
Writing history
Permanently save your work
$ git commit
Initial commit for workshop
Here follows a longer description of what the commit did. In our case, we simply added a file for demonstration purposes.
Updating files
Tell Git about new changes
Same thing as adding new files (Git only cares about the content of files)
$ >>foo echo 'new content'
$ git add foo
$ git commit -m 'Updated file'
Browsing history
Graphical tool: gitk
ncurses: tig
$ git log
commit 7d266f1981b3156784a17a44ef7b46a62c72a21c
Author: Daniel Knittl-Frank <S1010454…@students.fh-hagenberg.at>
Date: Sun Feb 26 18:21:51 2012 +0100
Updated file
commit 01d9322c973c5055ec630a7e9120ac90f6862269
Author: Daniel Knittl-Frank <S1010454…@students.fh-hagenberg.at>
Date: Sun Feb 26 18:21:31 2012 +0100
Initial commit for workshop
Here follows a longer description …
Working Copy Status
Shows list of modified, new and staged files
Also reminds you of important commands
$ git status
# On branch master
nothing to commit (working directory clean)
Inspecting changes
Show current changes
Show differences between commits
Show differences between branches
$ git diff
$ git diff HEAD^ HEAD
$ git diff branch1 branch2
Branching history
Try out new features
In an ideal world: one branch per feature
Branches are cheap, use them often
Branches can be deleted
$ git branch newbranch
$ git checkout newbranch
$ git branch
master
* newbranch
Merging changes
Integrate changes from a branch
Integrate changes from others (more later)
$ git merge newbranch
Resolving merge conflicts
Merges don’t always go well
Git inserts conflict markers into conflicting files
Remove conflicts and add that state of the file with git add
$ cat foo
<<<<<<< HEAD
current content
=======
branch content
>>>>>>> newbranch
$ vim foo
$ git add foo
$ git commit
Forking existing projects
Get a copy of the repository
fetch
/pull
to update your copy
$ git clone git://git.kernel.org/pub/scm/git/git.git git
$ cd git
…
$ git fetch
$ git pull
Collaborating
Use Git’s distributed powers
Pull branches from remote repositories
Define bookmarks for repositories
$ git pull git://example.com/repo.git feature
$ git remote add repo git://example.com/repo.git
$ git pull repo feature
Publishing your projects
Free hosters for open source projects:
‘Quick and dirty’ method: git daemon --verbose --export-all --base-path=. ./.git
$ ssh user@server
$ git init --bare /path/to/repo
$ logout
$ git push user@server:/path/to/repo
Graphical Clients / Frontends