Top Banner
Introduction To Git Workshop Tom Aratyn
64

Introduction To Git Workshop

May 06, 2015

Download

Technology

themystic_ca

A workshop I ran at FSOSS 2013 introducing new users to git through a series of hands on exercises.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction To Git Workshop

Introduction To Git Workshop

Tom Aratyn

Page 2: Introduction To Git Workshop

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

Page 3: Introduction To Git Workshop

About Me

Django and JavaScript Developer

Founder @ The Boulevard PlatformEngineer @ FoxyProxy

Created open source projects:BitBucket Release Note Generatordjango-email-changerExploit Me Suite

Page 4: Introduction To Git Workshop

Why Git?

Git is• Small• Fast• Distributed• Free & Open Source• Trusted• Linux• Homebrew• Everyone on GitHub + Gitorious +

Page 5: Introduction To Git Workshop

Not Just For Code (but mainly for code)

http://government.github.com

Page 6: Introduction To Git Workshop
Page 7: Introduction To Git Workshop

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

Page 8: Introduction To Git Workshop

Making A Git Repository

Normal Repository Bare Repository Cloned Repository

Page 9: Introduction To Git Workshop

Normal Repository A git repository with a working directory

Page 10: Introduction To Git Workshop

Normal Repository Exercise

$ git init workshop.normal.git

Page 11: Introduction To Git Workshop

Bare Repository

A git repo without a working directory(this is what you want on the repo)

Page 12: Introduction To Git Workshop

Bare Repository Exercise

$ git init --bare workshop.bare.git

Page 13: Introduction To Git Workshop

Cloned RepositoryA normal repository is a copy of a remote repository and setup to work with it.

Page 14: Introduction To Git Workshop

Cloned Repository Exercise

$ git clone workshop.bare.git workshop.git$ cd workshop.git

Page 15: Introduction To Git Workshop

 

Image By: Scott Chacon, Pro Git

Page 16: Introduction To Git Workshop

Staging Files

Before a file can be added it must be staged

Page 17: Introduction To Git Workshop

Staging File Exercise

$ mvim index.html$ git add index.html

Page 18: Introduction To Git Workshop

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#

Page 19: Introduction To Git Workshop

Committing Files

Committing means that we want in the version control system.All staged files will be committed.

Page 20: Introduction To Git Workshop

Commit File Exercise

$ git commit –m "My initial commit"

Page 21: Introduction To Git Workshop

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

Page 22: Introduction To Git Workshop

The Log

Git keeps a log of everything you commit, who wrote authored it, and who committed it.

Page 23: Introduction To Git Workshop

View The Git Log Exercise

$ git log$ git log --graph$ gitk

Page 24: Introduction To Git Workshop

(Don’t worry there are better GUIs, more on that later)

Page 25: Introduction To Git Workshop

Why isn't your name there?

Page 26: Introduction To Git Workshop

The Parts Of A Log

List of commits and branch pointersA commit is made up of:• SHA1 id• Message• Files• Author• Committer

Page 27: Introduction To Git Workshop

Configuring git

git is configured in two places:~/.gitconfigyoureRepo/.git/config

Page 28: Introduction To Git Workshop

Configuring Git Exercise

$ git config --global user.name "Your Name"$ git config --global user.email [email protected]

Page 29: Introduction To Git Workshop

Configuring Git Exercise Result

$ cat ~/.gitconfig[user]

name = Tom Aratyn email = "[email protected]"

Page 30: Introduction To Git Workshop

Changing History

There are many ways to change history in git.

We're only going to look at one way:

Amend the last commit.

Page 31: Introduction To Git Workshop

Change The Last Commit Exercise

$ git commit --amend -m "initial commit with an html file"$ # has the author changed?$ gitk

Page 32: Introduction To Git Workshop

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)

Page 33: Introduction To Git Workshop

Update the Author Exercise

$ git commit --amend --reset-author# why did the screen change ?# type in ":wq" to leave vim.

Page 34: Introduction To Git Workshop

How To Remove A File?

What if we committed a file we no longer need can we get rid of it?

Yes & No

Page 35: Introduction To Git Workshop

Yes, You Can Remove ItFrom the current (and future) versions.

Page 36: Introduction To Git Workshop

No, It'll Be In Past VersionsThe whole point of version control is you can always recover old files.

Page 37: Introduction To Git Workshop

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"

Page 38: Introduction To Git Workshop

Practice: Adding a fileCreate another index.html and commit it

Page 39: Introduction To Git Workshop

Branching

Fast and easy branching is git's killer feature.

Branches let development progress on multiple fronts separately and simultaneously.

Page 40: Introduction To Git Workshop

Check Your Branch Exercise

$ git branch$ git branch –a$ # What's the difference between the two commands?

Page 41: Introduction To Git Workshop

Create A Branch Exercise

$ git branch workshop-example$ git branch$ # what branch are you on?

Page 42: Introduction To Git Workshop

Switch Branch Exercise

$ git checkout workshop-example$ git branch$ # now what branch are you on?

Page 43: Introduction To Git Workshop

Switch To A New Branch Immediately Exercise

$ git checkout -b fix-bug-123$ git branch$ gitk

Page 44: Introduction To Git Workshop

Making A Change On A Branch Exercise

$ # edit index.html$ git add index.html$ git commit -m "Added some initial html"

Page 45: Introduction To Git Workshop

Merging

Merging is really git's killer featureBecause branching without merging is pretty useless

See CVS

Page 46: Introduction To Git Workshop

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

Page 47: Introduction To Git Workshop

Two Three types of merges

1. Fast Forward Merge2. Basic Merge

a. Conflicted Merge

Page 48: Introduction To Git Workshop

Fast Forward Merge

Only available when the branch can be cleanly applied onto your current branch

Page 49: Introduction To Git Workshop

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

Page 50: Introduction To Git Workshop

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

Page 51: Introduction To Git Workshop

Basic Merge Exercise

$ git checkout master $ git merge fix-bug-123$ git log --graph --decorate --all

Page 52: Introduction To Git Workshop

Conflicted Merge Exercise

Prep

Change the same line on both branches(change the class on the same div)

Recall

git checkoutgit addgit commit

Page 53: Introduction To Git Workshop

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

Page 54: Introduction To Git Workshop

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.

Page 55: Introduction To Git Workshop

Pushing Exercise

$ # Recall that we cloned our bare repo$ git push origin master$ cd ../workshop.bare.git $ git log

Page 56: Introduction To Git Workshop

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

Page 57: Introduction To Git Workshop

Pulling Exercise

$ cd ../workshop.2.git$ git branch$ git pull$ git log

Page 58: Introduction To Git Workshop

How does pulling work?

Page 59: Introduction To Git Workshop

Tracking Branches

A tracking branch is a local branch which knows that updates to a remote branch should be applied to it.

Page 60: Introduction To Git Workshop

Tracking Branch Exercise

$ git checkout –t remotes/origin/fix-bug-123

Page 61: Introduction To Git Workshop

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

Page 62: Introduction To Git Workshop

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

Page 63: Introduction To Git Workshop

Questions?

Page 64: Introduction To Git Workshop

Thank You!

A link to this presentation will be onhttp://blog.tom.aratyn.name@[email protected] (I can email it to you)