2. Using Git
Basics and workflows
keskiviikkona 28. syyskuuta 2011
3. Git in a nutshell
• Distributed revision control system with an emphasis on speed
• Git does not use a centralized server (like CVS, Subversion)
• Developed by Linus Torvalds on 2005 for maintenance of Linux
kernel development
• “I name all my projects after myself. First Linux, now Git.”
('git' is British slang for stupid, childish person)
• “Take CVS as an example of what NOT to do”
• Goals
• Very high performance
• Simple design (“Git is a stupid content tracker” as Linus says)
• Strong support for non-linear development (branches)
• Fully distributed workflow
• Able to handle large projects (like Linux kernel)
keskiviikkona 28. syyskuuta 2011
5. Local VCS
• First generation of Version Control Systems
• Local database to handle versions
• No collaboration between developers
keskiviikkona 28. syyskuuta 2011
6. Centralized VCS
• Single server contains all the versioned files
• CVS, Subversion
• Advantages
• Everyone knows everyone else progress
• Easier to administer
• Downsides
• Single point of failure
keskiviikkona 28. syyskuuta 2011
7. Distributed VCS
• Clients fully mirror the repository
• All revision data lies in .git directories
• Git, Mercurial, Bazaar
• Advantages
• Every checkout is really a full
backup of all the data
• Parallel workflows within
the same project
• Downsides
• Not (yet!) ;-)
keskiviikkona 28. syyskuuta 2011
8. Using Git
Concept
keskiviikkona 28. syyskuuta 2011
9. Git vs. any other CVS
• Other systems store data as changes to a base version of each file
• Git stores data as snapshots of the project over time
keskiviikkona 28. syyskuuta 2011
10. Git vs. Centralized CVS
• Most operations are local
• Incredible speed while doing operations
• You can continue your work while been
offline (commit data, rollback, etc)
keskiviikkona 28. syyskuuta 2011
11. The Three States
• Files are always committed, staged or modified
• Can be very useful in large projects
• The whole staging phase can be skipped entirely
keskiviikkona 28. syyskuuta 2011
12. Tracked and untracked files
• Each file in working copy directory (your project folder) can
be tracked or untracked
• Tracked files can be committed (unmodified), modified or staged
• Untracked files are everything else in your working copy directory
• E.g. in Drupal project sites/default/settings.php would be untracked
• Untracked files can be defined in .gitignore file
keskiviikkona 28. syyskuuta 2011
13. Using Git
Basic commands
keskiviikkona 28. syyskuuta 2011
14. Basic commands
Create clean git project directory
$ git init
In most cases (like Drupal based) clone an existing repository
$ git clone git://git.drupal.org/project/drupal.git myfolder
keskiviikkona 28. syyskuuta 2011
15. Basic commands
See git config
$ git config --list
$ git config --global user.name "Lari Vaartio"
$ git config --global user.email "lari.vaartio@mearra.com"
Check status of your project
$ git status
Check log info
$ git log
Check local branches and all branches (remote and local)
$ git branch OR $ git branch -r
keskiviikkona 28. syyskuuta 2011
16. Creating branch
Let’s create our own branch
First select release (tag) you are going to use
$ git tag
$ git checkout DRUPAL-7-0
Check log and branch info
$ git log
$ git branch
Create new branch
$ git checkout -b mearra
keskiviikkona 28. syyskuuta 2011
17. Adding files
Let’s add some custom code
$ drush dl addthis
$ git status
$ git add .
$ git commit -m ‘added addthis module’
$ git log
Let’s make some modifications
$ pico sites/all/modules/addthis/README.txt
$ git add sites/all/modules/addthis/README.txt
OR to skip staging entirely
$ git commit -a -m ‘Contact information updated’
keskiviikkona 28. syyskuuta 2011
18. Discarding changes
To discard changes which are staged but not committed yet
$ git reset HEAD sites/all/modules/addthis/README.txt
To discard changes which are modified but not staged
$ git checkout sites/all/modules/addthis/README.txt
To rollback previous commit grab a commit from git log
$ git reset 521fa42e (goes to the commit but keeps files)
OR
$ git reset --hard 521fa42e (delete files too)
keskiviikkona 28. syyskuuta 2011
19. Ignoring files
Let’s add a file which should be untracked
$ cp sites/default/default.settings.php sites/default/
settings.php
$ git status
$ echo "sites/default/settings.php" >> .gitignore
$ git status
$ git add .gitignore
$ git commit -m 'gitignore file added'
keskiviikkona 28. syyskuuta 2011
20. Remote Repositories
Show remote repositories
$ git remote
Add a new one for the project
$ git remote add mearrarepo git@github.com:vaartio/mearrarepo.git
$ git push <repositoryname> <branchname>
Commit new data to remote repository
$ git push mearrarepo mearra
Pull data from remote repository
$ git fetch mearrarepo mearra
$ git pull mearrarepo mearra (fetch and merge)
keskiviikkona 28. syyskuuta 2011
21. Merging
Merge new Drupal core version with mearra branch
$ git merge CVS
$ git log
keskiviikkona 28. syyskuuta 2011
22. Using Git
Branching
keskiviikkona 28. syyskuuta 2011
23. Branching
• Branch is a snapshot
Snapshots Commits
keskiviikkona 28. syyskuuta 2011
24. Branching
$ git branch testing
Creates a new pointer at the same commit you’re currently on.
keskiviikkona 28. syyskuuta 2011
25. Branching
$ git checkout testing
This moves HEAD to point to the testing branch.
keskiviikkona 28. syyskuuta 2011
26. Branching
Make a change and commit it.
$ vim test.rb
$ git commit -a -m 'made a change'
keskiviikkona 28. syyskuuta 2011
27. Branching
Let’s switch back to the master branch.
$ git checkout master
This moves HEAD to master branch and reverts the files in your
working directory back to the snapshot that master points to.
keskiviikkona 28. syyskuuta 2011
28. Branching
Let’s make changes to the master branch.
$ vim test.rb
$ git commit -a -m 'made other changes'
keskiviikkona 28. syyskuuta 2011
29. Merging branches
Let’s merge iss53 branch to the master.
$ git merge iss53
Step 1 Step 2
Step 3
keskiviikkona 28. syyskuuta 2011
30. Using Git
Different workflows
keskiviikkona 28. syyskuuta 2011
31. Centralized Workflow
No differs from CVS or Subversion
keskiviikkona 28. syyskuuta 2011
32. Integration Manager Workflow
1. The project maintainer pushes to their public repository.
2. A contributor clones that repository and makes changes.
3. The contributor pushes to their own public copy.
4. The contributor sends the maintainer an e-mail asking them to pull changes.
5. The maintainer adds the contributor’s repo as a remote and merges locally.
6. The maintainer pushes merged changes to the main repository.
keskiviikkona 28. syyskuuta 2011
33. Dictator and Lieutenants Workflow
1. Regular developers work on their topic branch and rebase their work on top of master. The
master branch is that of the dictator.
2. Lieutenants merge the developers’ topic branches into their master branch.
3. The dictator merges the lieutenants’ master branches into the dictator’s master branch.
4. The dictator pushes their master to the reference repository so the other developers can
rebase on it.
keskiviikkona 28. syyskuuta 2011
34. Summary
• Git is distributed VCS
• Git stores snapshots not changes
• Nearly all operations are local and therefore
they are very fast
• Branch is a snapshot, not files in a
separated directory
• Git supports multiple remote repositories
which enables different workflows
• Git is powerful
keskiviikkona 28. syyskuuta 2011