Source code control is the most important practice that a software professional can do. This presentation introduces Git, the modern, distributed, version control designed for speed and efficiency.
This was delivered for a local Drupal user group.
I originally gave this presentation at the Drupal Self-Help Group meetup, in May 2012.
1. Getting Started with Git
5/17/2012
This presentation is at:
http://sn.im/startgit
If you're on Google Docs, make sure to read the
'speaker notes' for this presentation.
Rick Umali
rickumali@gmail.com
http://tech.rickumali.com/
2. Questions I Plan to Answer
What is source control?
What is the big deal with Git?
Can you show me a little Git?
Commits. Branches. Merges. Remote repos.
3. What is Source Control?
A mechanism to track changes in source code.
Used for version history, auditing, and
recovery.
Source code control is the most important
practice a coding professional can do.
6. Git
Git is an open source, distributed version
control system designed for speed and
efficiency.
● Freedom
● No "server" required
● Unique architecture
9. First Time Use: Set Up
Tell Git who you are!
% git config --global user.name "Rick Umali"
% git config --global user.email "rickumali@gmail.com"
Future 'commits' will show this information.
% git config --list
See "git config --help" for more configurations.
10. Creating a "Repository"
Let's pretend we're developing a Drupal
module.
% cd web/sites/all/modules
% mkdir dumpstamp
% cd dumpstamp
% git init
"git init" creates the entire Git repository. No
server interaction required!
11. Committing Your First File
To 'commit' means 'to save the state' of your
work. You must first 'add' this change to the
'staging area'.
% vi README.txt
% git add README.txt
% git commit -m "First commit. README file."
Use 'git help' to learn all the switches. -m
stands for message.
12. Looking at the Repository
% git log
Each 'commit' contains an ID, along with the
author information from earlier, and a time
stamp.
% gitk
GUI tools can help you visualize the 'repo.'
13. Adding More Files
% vi dumpstamp.info dumpstamp.module
% git status
% git add .
% git commit
This second commit saves the work of adding
two files by using 'git add .'.
The .module and .info files are the two required
files for every Drupal module.
15. Examining Changes to Files
% vi dumpstamp.module
% git status
% git diff
% git add dumpstamp.module
% git commit
% git log
The above is a typical 'workflow'.
Git offers suggestions and hints as you use
it.
16. Doing More Changes
% vi dumpstamp.module
% git diff
% git commit -a
Or even:
% git commit -a -m "Commit message."
Another typical 'workflow'.
17. Looking at the Log Again
The history can be examined different ways.
% git log
% git log --format=short
% git log --format=oneline
% git log --oneline
18. Revisiting History
You can 'revisit' any point of your history.
% git checkout SHAID
Every commit is known by its SHA ID.
This is the first step in making a branch! (Use
git checkout master to revert.)
19. Branching and Merging Next, But...
What we have covered so far is probably 70-
80% of what you will do with git.
Adding and committing files are the heart of
git (and any version control system).
21. Branching: git checkout -b
% git checkout -b BRANCH SHA1
Make some edits on a change below the
master, then commit.
git branch makes a branch from the branch
you're on (default branch is 'master').
24. Branching: Making a Branch
git checkout -b branch1 SHA1
master SHA 2B
SHA 1A branch1
NOTE: This command is a short cut for: git branch branch1 name_of_branch ,
then git checkout branch1 .
25. Branching: Changes on the Branch
(Make changes in branch1.)
git commit
master SHA 2B SHA 3C branch1
SHA 1A
27. Merging
Bringing two branches together.
Git calculates the correct 'merge base'.
First 'checkout' the branch you want to merge
into (typically master), then 'merge' the branch.
% git checkout master
% git merge BRANCH
33. Remote Branches
You can 'browse' public Git repositories for
code that you want to examine or use.
You can upload a local Git repository to a
public Git repository.
35. Browsing and Grabbing Code
git clone git@github.com:rickumali/RickUmaliVanityWebsite.
git
36. Uploading Code (to Github)
Create a key pair on your machine.
Create a repository (on Github).
Add a 'remote' (via git remote add).
Upload your code (via git push).
40. After An Upload (to Github)
git clone git@github.com:rickumali/DumpStamp.git
41. Next Steps
Install Git.
Commit your code changes frequently.
Log verbosely (in commit messages).
Experiment (branch) often.
42. Resources
http://git-scm.org/
Both "Pro Git" book, and Git reference
http://gitref.org/
A "quicker" Git reference
http://www-cs-students.stanford.edu/~blynn/gitmagic/
"Friendlier" Git walk-through (git magic).
http://drupal.org/node/803746
A workflow for using Git with Drupal.
http://www.mail-archive.com/dri-devel@lists.sourceforge.
net/msg39091.html
Linus on "clean history."