Introduction To Git Workshop Tom Aratyn
May 06, 2015
Introduction To Git Workshop
Tom Aratyn
Get Git
• On Matrix• I have accounts for you
• On Windows• http://msysgit.github.io
• On OS X• brew install git
• On Linux• sudo apt-get install git
• Otherwise• http://git-scm.com
About Me
Django and JavaScript Developer
Founder @ The Boulevard PlatformEngineer @ FoxyProxy
Created open source projects:BitBucket Release Note Generatordjango-email-changerExploit Me Suite
Why Git?
Git is• Small• Fast• Distributed• Free & Open Source• Trusted• Linux• Homebrew• Everyone on GitHub + Gitorious +
Not Just For Code (but mainly for code)
http://government.github.com
About Today
What we will cover• Creating Repos• Checking Out• Committing• Basic Branching • Basic Merging• Pushing & Pulling
What we won't• git reset
• Changing history
• Rebasing• Adding/Removing
Remotes• Partial Staging• Fetch• Tags
Making A Git Repository
Normal Repository Bare Repository Cloned Repository
Normal Repository A git repository with a working directory
Normal Repository Exercise
$ git init workshop.normal.git
Bare Repository
A git repo without a working directory(this is what you want on the repo)
Bare Repository Exercise
$ git init --bare workshop.bare.git
Cloned RepositoryA normal repository is a copy of a remote repository and setup to work with it.
Cloned Repository Exercise
$ git clone workshop.bare.git workshop.git$ cd workshop.git
Image By: Scott Chacon, Pro Git
Staging Files
Before a file can be added it must be staged
Staging File Exercise
$ mvim index.html$ git add index.html
What’s The Status Of My Files Right Now?
$ git status# On branch master## Initial commit## Changes to be committed:# (use "git rm --cached <file>..." to unstage)## new file: index.html#
Committing Files
Committing means that we want in the version control system.All staged files will be committed.
Commit File Exercise
$ git commit –m "My initial commit"
Commit File Exercise Result
Committer: Tom Aratyn <[email protected]>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly:
git config --global user.name "Your Name" git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 foo
The Log
Git keeps a log of everything you commit, who wrote authored it, and who committed it.
View The Git Log Exercise
$ git log$ git log --graph$ gitk
(Don’t worry there are better GUIs, more on that later)
Why isn't your name there?
The Parts Of A Log
List of commits and branch pointersA commit is made up of:• SHA1 id• Message• Files• Author• Committer
Configuring git
git is configured in two places:~/.gitconfigyoureRepo/.git/config
Configuring Git Exercise
$ git config --global user.name "Your Name"$ git config --global user.email [email protected]
Configuring Git Exercise Result
$ cat ~/.gitconfig[user]
name = Tom Aratyn email = "[email protected]"
Changing History
There are many ways to change history in git.
We're only going to look at one way:
Amend the last commit.
Change The Last Commit Exercise
$ git commit --amend -m "initial commit with an html file"$ # has the author changed?$ gitk
Author Vs. Committer
Git is made from from the ground up for multiple developer projects (Linux).Large projects often distinguish between the author (who wrote the patch/code) and the committer (who let it into the blessed repository)
Update the Author Exercise
$ git commit --amend --reset-author# why did the screen change ?# type in ":wq" to leave vim.
How To Remove A File?
What if we committed a file we no longer need can we get rid of it?
Yes & No
Yes, You Can Remove ItFrom the current (and future) versions.
No, It'll Be In Past VersionsThe whole point of version control is you can always recover old files.
Remove A File Exercise
$ git rm index.html$ ls$ #Notice how the file is now gone$ git status $ #Notice how the file is staged$ git commit -m "Removed index.html"
Practice: Adding a fileCreate another index.html and commit it
Branching
Fast and easy branching is git's killer feature.
Branches let development progress on multiple fronts separately and simultaneously.
Check Your Branch Exercise
$ git branch$ git branch –a$ # What's the difference between the two commands?
Create A Branch Exercise
$ git branch workshop-example$ git branch$ # what branch are you on?
Switch Branch Exercise
$ git checkout workshop-example$ git branch$ # now what branch are you on?
Switch To A New Branch Immediately Exercise
$ git checkout -b fix-bug-123$ git branch$ gitk
Making A Change On A Branch Exercise
$ # edit index.html$ git add index.html$ git commit -m "Added some initial html"
Merging
Merging is really git's killer featureBecause branching without merging is pretty useless
See CVS
Merging Process
1. Go to the branch you want to merge into• Often the branch you branched off of.• Usually "master" or "develop"
2. Do the merge
Two Three types of merges
1. Fast Forward Merge2. Basic Merge
a. Conflicted Merge
Fast Forward Merge
Only available when the branch can be cleanly applied onto your current branch
Fast Forward Merge Exercise
$ # (assuming you have a change on fix-bug-123 - use gitk to check)$ git checkout master$ git merge fix-bug-123$ gitk
Basic Merge Exercise
Prep
Add add a div on the master branchChange the title on the fix-bug-123 branch
Recall
git checkoutgit addgit commit
Basic Merge Exercise
$ git checkout master $ git merge fix-bug-123$ git log --graph --decorate --all
Conflicted Merge Exercise
Prep
Change the same line on both branches(change the class on the same div)
Recall
git checkoutgit addgit commit
Conflicted Merge Exercise
$ git checkout master$ git merge fix-bug-123$ git status$ # edit index.html$ git add index.html$ git commit $ git log --graph --decorate --all
Sharing Is Caring
So far everything we've done is on the same repo but projects need to be shared.
Git lets you push your changes to others and pull the changes others made.
Pushing Exercise
$ # Recall that we cloned our bare repo$ git push origin master$ cd ../workshop.bare.git $ git log
Pulling Exercise
Prep
1. Clone the bare repo again• Call it workshop.2.git
2. Commit a change to workshop.git
3. Push the change
Recall
git clonegit addgit commitgit push
Pulling Exercise
$ cd ../workshop.2.git$ git branch$ git pull$ git log
How does pulling work?
Tracking Branches
A tracking branch is a local branch which knows that updates to a remote branch should be applied to it.
Tracking Branch Exercise
$ git checkout –t remotes/origin/fix-bug-123
About Today
What we covered• Creating Repos• Checking Out• Committing• Basic Branching • Basic Merging• Pushing & Pulling
What we didn't• git reset
• Changing history
• Rebasing• Adding/Removing
Remotes• Partial Staging
Where to next?
Learn more at from "Pro Git"http://git-scm.com/book
Start Your Project:Free Open Source Repos
http://github.com
Free Private Reposhttp://bitbucket.org
GUI: http://SourceTreeApp.com
Questions?
Thank You!
A link to this presentation will be onhttp://blog.tom.aratyn.name@[email protected] (I can email it to you)