Git - A fast distributed revision control system

Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Git
A fast distributed revision control system
Nils Moschüring
PhD Student (LMU)
Nils Moschüring PhD Student (LMU) , Git
1
Outline
The three W’s
Overview of gits structure
1
The three W’s
What?
Why?
Workflow and nomenclature
2
Overview of gits structure
Structure
Branches
3
Using git
Setting up
Working
Sharing
4
Final stuff
Nils Moschüring PhD Student (LMU) , Git
Using git
Final stuff
2
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
What is a version control system?
A system which monitors data continuously
’Monitors‘ means that it will detect any changes to the data and will be
able to tell you about them.
At every point during your work you can tell the system to take a snapshot
(a record of the current status of the data).
You can let others change your data and these changes will also be
recorded.
A friend with benefits (on top of the above)
Everything is saved with a timestamp and user information (making it easy
to pinpoint and blame the source of a mistake).
All changes can easily be reverted to any previous state.
You can even revert to a previous version, fix an error and automatically
redo all the other good stuff!
Nils Moschüring PhD Student (LMU) , Git
4
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Why do I need it?
Single User:
This functionality greatly simplifies the development process of digital
products.
Especially the development of software.
Multi User:
Everybody can change whatever he wants, without consulting anybody
else.
All the modifications by all the different users will be merged to one final
result automatically.
If they don’t overlap.
Different branches are supported.They enable you to work on a specific
subproblem separately. Of course, these can later be merged without
trouble.
If they don’t overlap.
Nils Moschüring PhD Student (LMU) , Git
5
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Workflow - Commit
Nils Moschüring PhD Student (LMU) , Git
6
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Workflow - Commit
Nils Moschüring PhD Student (LMU) , Git
7
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Workflow - Checkout
Nils Moschüring PhD Student (LMU) , Git
8
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Workflow - Merge
Nils Moschüring PhD Student (LMU) , Git
9
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Workflow - Branches
Nils Moschüring PhD Student (LMU) , Git
10
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Git is a distributed RCS
Nils Moschüring PhD Student (LMU) , Git
12
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Working Copy - Index - Repository
Working
Directory
checkout
Nils Moschüring PhD Student (LMU) , Git
Index
add reset
Repository
commit
13
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Branches
How do they work?
different versions of the same overall functionality (overlaps)
experimental stuff, tryouts
parallel development on the same trunk
What branches do I have and why:
Run git branch -a
Git keeps copies of all branches on your source repository marked with a
remotes/ prefix. If you run the git fetch command, these local copies will be
updated. git fetch makes changes from a different repository available in your
repository.
You can create new remotes with the git remote command. You can set an alias
(as ‘origin’ in the cloned case).
The branches marked as remotes/ can not be checked out. You have to create a
local branch which tracks the remote one.
‘Tracking’ means, that git pull and git push will use these branches as a
default.
Nils Moschüring PhD Student (LMU) , Git
14
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Branches - workflow example:
new features & multi users
Testing
V1
branch
Master
V1
V2
V2
merge
V3
V3
merge
V4
V5
commits
Nils Moschüring PhD Student (LMU) , Git
15
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Branches - workflow example:
different versions
microwave
V1
branch
oven
V1
V2
branch
grill
Nils Moschüring PhD Student (LMU) , Git
V1
V2
V3
merge
V3
V4
V5
merge
V2
V3
16
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: Setting up
Configuring Git
Run:
git config --global user.name "Bob"
git config --global user.email "[email protected]"
Setting up a local repository
git init
Copying (Cloning) an existing repository
git clone GIT-URL
Possible GIT-URLs (only ssh and local shown):
ssh://[user@]host.xz[:port]/path/to/repo.git/
[user@]host.xz:path/to/repo.git/
/path/to/repo.git/
Nils Moschüring PhD Student (LMU) , Git
18
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: working
The git checkout command
Checks out a branch: Sets your repository to the last commit of the branch
(called the HEAD), sets your index and your working directory to the
desired branch data.
Important Command line argument:
-b Create a new branch and switch to it
The git add command
Puts local changes into the index on a file by file basis.
The git diff command
Shows the difference between your working copy and the index.
The git status command
Shows the current status of your working directory, index and repository.
Nils Moschüring PhD Student (LMU) , Git
19
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: working
The git commit command
Puts the changes in the index into the repository creating a new
checkpoint (commit).
Important command line argument:
-a Includes adding all changes to the index.
-m Followed by a string (use quotes!); Sets this string as the commit message.
The git branch command
Creates a new branch or shows the existing branches.
Important command line arguments:
-a Shows all existing branches
-d Deletes a branch (verifying that the changes are in the current branch)
-D Delete a branch without verifications
Example: git branch my_testing remotes/origin/testing
Creates a new local branch named ‘my testing’ which is tracking the
branch named ‘testing’ on the origin repository.
Nils Moschüring PhD Student (LMU) , Git
20
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: working
The git merge command
Normally you will use it to merge a branch you will abandon back into your
main branch. To do that, checkout the main branch and do
git merge soon-to-be-abandoned-branch
If there are conflicts (the two branches differ in the same line), git will
leave conflict markers. They look like this:
<<<<<<<
Content
=======
Content
>>>>>>>
master
of line x in branch master
of line x in branch local
local
Resolve them and commit the result!
The gitk command
This will show a nice graphical representation of your repository, the branches
and their relations. A good alternative for Mac OS X is GitX which can be found
on MacPorts.
Nils Moschüring PhD Student (LMU) , Git
21
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: sharing
The git pull command
Consists of the git fetch and the git merge command as follows
The first parameter can be a repository GIT URL. It is used to fetch from.
The results are normally stored in the local ‘remotes/remote-alias/’
branches.
The second parameter is the branch on the foreign repository to be used
in the merge command.
If you’ve set up your tracking branches, git will automatically know all of
the above and a git pull without any arguments updates your local
branch to the respective branch on the remote repository.
This command always modifies the currently checked out branch!
Of course this could cause merge conflicts...
Nils Moschüring PhD Student (LMU) , Git
22
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: sharing
The git push command
Tries to update a branch on a remote repository.
Normally works only for fast forwards, i.e. merges with no conflicts (there
are good reasons)
You can force a push, but if you do, it will NOT do a merge!
If the push doesn’t work, the normal procedure would be to do a pull first,
resolve the problems and then push the new commit, which is then
directly descendent from the current status on the repository.
Nice feature: hooks
You can make interesting things happen to a repository every
time you push into it... http://www.kernel.org/pub/software/scm/git/docs/git-push.html
Nils Moschüring PhD Student (LMU) , Git
23
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: advanced git commands
The git rebase command
Rewriting of history
Squashing of commits
Creating a linear history
The git grep command
Improved searching in repositories
Follows the same basic syntax as Unix grep
The git stash command
Puts your changes to a safe place and resets the working directory and
index
Handy for quick looks into different branches without having to create a
commit
Nils Moschüring PhD Student (LMU) , Git
24
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: advanced git commands, rebase
Testing
V2
V1
V3
merge
branch
Master
V1
V3
V2
V4
rebase
V1
V2
Nils Moschüring PhD Student (LMU) , Git
V3
V4
V1
V2
V3
25
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Using git: advanced git commands
The git rebase command
Rewriting of history
Squashing of commits
Creating a linear history
The git grep command
Improved searching in repositories
Follows the same basic syntax as Unix grep
The git stash command
Puts your changes to a safe place and resets the working directory and
index
Handy for quick looks into different branches without having to create a
commit
Nils Moschüring PhD Student (LMU) , Git
26
Outline
The three W’s
Overview of gits structure
Using git
Final stuff
Final stuff
Of course there are many more really useful commands you should use,
please discover them on your own. References I used:
http://book.git-scm.com/index.html
http://www.kernel.org/pub/software/scm/git/docs/git.html
Take-Home-Message
USE GIT
Nils Moschüring PhD Student (LMU) , Git
28