1 of 97

Git & GitHub Basics

KYOSS Presentation on Git

DVCS for everyone

By: Alan Blount & Jeff Squyres

Oct 2012

http://goo.gl/qr2JS

2 of 97

Why Is Version Control Important?

We all know about...

but what about this?

FAIL

FAIL

Version control is important

because "FAIL" happens

3 of 97

Why Is Version Control Important?

Or more likely, when working on a project

have you ever done anything like this?

4 of 97

Version Control to the Rescue

A Version Control System (VCS) would fix that

  • Never lose data
  • Internal verification on integrity
  • Historical version retained
  • History of who did what
  • Commit messages
  • Diffs / patches
  • Branches & tags
  • Immutable reference

5 of 97

What is Git?

Git is an open source,

distributed version control system designed for speed and efficiency

6 of 97

What is Git?

Git is an open source,

distributed version control system designed for speed and efficiency

http://git-scm.com/

Easy to install

(covered later)

7 of 97

What is Git?

Git is an open source,

distributed version control system designed for speed and efficiency

(almost) everything is local

which means

everything is fast

backups are "built in"

work offline

8 of 97

What is Git?

Git is an open source,

distributed version control system designed for speed and efficiency

  • Again, most of your work is local (no network)
  • Branching & merging is painless (really!)
  • Pulling from and pushing to remotes, network interactions are compressed and minimal over several possible network protocols (including SSH)

Use it how you want to!

9 of 97

Enough talking -- let's do Git

G

10 of 97

Pre-step 0:

Do you have Git installed?

See if you have Git already installed:

$ which git

git not found

$

If you see "git not found",

you need to install Git

11 of 97

Pre-step 1:

Install Git (if necessary)

Linux

Red Hat / Centos

$ sudo yum install git

Debian / Related

$ sudo apt-get install git

OS X

Mac Ports

$ sudo port install git-core

Homebrew

$ brew install git

12 of 97

Now on to the good stuff...

13 of 97

Configure your Git

Tell Git who you are

$ git config --global user.name "Bob Smith"

$ git config --global user.email "bobsmith@gmail.com"

Tell Git that you like color

$ git config --global color.ui auto

Tell Git that you like nice diffs

$ git config --global merge.conflictstyle diff3

Content from presentation by Jason Axelson

14 of 97

Create a local Git repo

Make a directory for all your git repos

$ cd

$ mkdir git

$ cd git

Make a git repo

$ mkdir kyoss-git-workshop

$ cd kyoss-git-workshop

$ git init

Happy

Panda!

15 of 97

Make some files

Create two text files

$ echo "hello, world" > hello.txt

$ echo "goodbye, world" > goodbye.txt

Let's see what git thinks about these files

$ git status

What does it show?

(notice the pretty colors in the output)

16 of 97

Tell Git about these files

Tell Git about the "hello.txt" file

$ git add hello.txt

Now run "git status" again

$ git status

What does it show?

Any guesses as to why it says that?

17 of 97

Tell Git about these files

Tell Git about the "goodbye.txt" file

$ git add goodbye.txt

Now run "git status" again

$ git status

Now git knows about both files

They are in the "Staging Area"

18 of 97

Now commit the files to the repo

"Commit" (save) both files to the Git repo

$ git commit

You'll be prompted for a comment / message

Or

$ git commit -m "Add hello/goodbye"

19 of 97

Look at what you've done

What does status say now?

$ git status

All clear!

20 of 97

What exactly have we done?

Working directory

Git

repo

Git

staging area

git commit

git add

git status

21 of 97

Now let's change a file

$ echo "hello again" >> hello.txt

What will status show?

$ git status

(notice the pretty color in the status output)

22 of 97

Umm... what did I change again?

$ git diff

diff --git a/hello.txt b/hello.txt

index 4b5fa63..97a2a03 100644

--- a/hello.txt

+++ b/hello.txt

@@ -1 +1,2 @@

hello, world

+hello again

$

These numbers will be different for you

23 of 97

Umm... what did I change again?

$ git diff

diff --git a/hello.txt b/hello.txt

index 4b5fa63..97a2a03 100644

--- a/hello.txt

+++ b/hello.txt

@@ -1 +1,2 @@

hello, world

+hello again

$

You changed hello.txt by adding one line

24 of 97

git status and diff

Working directory

Git

repo

Git

staging area

git status

git diff

git diff --cached

25 of 97

10 rules of committing

1. Always "git status" and "git diff" before commit.

2. Always "git status" and "git diff" before commit.

3. Always "git status" and "git diff" before commit.

4. Always "git status" and "git diff" before commit.

5. Always "git status" and "git diff" before commit.

6. Always "git status" and "git diff" before commit.

7. Always "git status" and "git diff" before commit.

8. Always "git status" and "git diff" before commit.

9. Always "git status" and "git diff" before commit.

10. Always "git status" and "git diff" before commit.

1 rule

26 of 97

Time to "commit" again

Ok, let's commit our change to hello.txt

$ git commit

Or

$ git commit -m "add more to hello"

27 of 97

Time to "commit" again

Hey.. why didn't it work?

no changes added to commit (use "git add" and/or "git commit -a")

28 of 97

Add the change, then commit

Working directory

Git

repo

Git

staging area

git add

$ git add hello.txt

$ git commit

git commit

29 of 97

Make another change

$ echo "hello hello" >> hello.txt

$ git status

...output...

$ git diff

...output...

All that output makes sense

30 of 97

Now add, but do NOT commit

$ git add hello.txt

Remember the rule:

Run status and diff before commit!

$ git status

# modified: hello.txt

Ok, that looks good

31 of 97

Now add, but do NOT commit

$ git add hello.txt

Remember the rule:

Run status and diff before commit!

$ git diff

$

Wait... what?

32 of 97

We added the change,

so diff is empty

Working directory

Git

repo

Git

staging area

git status

git diff

git add

33 of 97

Make yet another change

$ echo "more hello" >> hello.txt

$ git status

Notice that hello.txt shows up TWICE

1. Changes to be committed

2. Changes not staged for commit

34 of 97

status lists changes

between all three

Working directory

Git

repo

Git

staging area

git status

git add

git commit

Changes not

staged for commit

Changes to be committed

35 of 97

Let's commit that

$ git add hello.txt

$ git status

$ git commit -m "I love hello"

Verify that everything is committed

$ git status

36 of 97

Be lazy: use commit -a

You can skip "git add"

$ echo "greetings" >> hello.txt

$ git status

$ git commit -a -m "Some variety"

Verify that everything is committed

$ git status

37 of 97

Let's look at the history / log

See all the commits we've done

$ git log

...lots of output...

The log command has lots of options

$ git log --oneline

38 of 97

Sidenote: git help

git help is your friend

$ git help

$ git help add

$ git help status

$ git help diff

$ git help commit

$ git help log

When in doubt, git help <foo>

39 of 97

One log entry

commit 21d0fddb739e2528ff03e40040d02ae7a0061272

Author: Bob Smith <bobsmith@gmail.com>

Date: Wed Oct 10 19:20:37 2012 -0400

Add hello/goodbye

40 of 97

One log entry

commit 21d0fddb739e2528ff03e40040d02ae7a0061272

Author: Bob Smith <bobsmith@gmail.com>

Date: Wed Oct 10 19:20:37 2012 -0400

Add hello/goodbye

Unique identifier for this commit

41 of 97

One log entry

commit 21d0fddb739e2528ff03e40040d02ae7a0061272

Author: Bob Smith <bobsmith@gmail.com>

Date: Wed Oct 10 19:20:37 2012 -0400

Add hello/goodbye

Who made the commit

42 of 97

One log entry

commit 21d0fddb739e2528ff03e40040d02ae7a0061272

Author: Bob Smith <bobsmith@gmail.com>

Date: Wed Oct 10 19:20:37 2012 -0400

Add hello/goodbye

When they made the commit

43 of 97

One log entry

commit 21d0fddb739e2528ff03e40040d02ae7a0061272

Author: Bob Smith <bobsmith@gmail.com>

Date: Wed Oct 10 19:20:37 2012 -0400

Add hello/goodbye

Log message for this commit

44 of 97

Let's look at the history / log

Show the most recent commit (the "HEAD")

$ git show

Show a specific commit

$ git log 21d0

$ git show 21d0

These numbers will be different for you

45 of 97

GitHub

The real power of git: its distributed nature

GitHub is a public, free Git repo hosting web site

Everyone has a GitHub account, right?

46 of 97

What is GitHub?

The reason it's a "Hub" is that if you think about a "Hub and Spokes" topology, you can centralize distribution via GitHub.

GitHub

47 of 97

GitHub

GitHub has a great web interface

- Create and destroy Git repos

- See git log entries and diffs

- ...etc.

Also accessible via the git CLI interface

48 of 97

What does GitHub Offer?

- Free for Open public repositories

- Reasonably priced Pay accounts for Private

- Web based code/commit logs & comments

- Basic Issue/Feature tracking system

- Basic Wiki for all projects

- Public, brandable website tied to repository

- Collaboration tools

49 of 97

Do I have to use GitHub?

No!

GitHub is an excellent solution for some problems, but you can use git:

  • locally (only)
  • on your local network
  • with some other service

There are many alternatives to GitHub�with many feature sets.

?

50 of 97

Let's use GitHub!

Create a repository on GitHub

https://help.github.com/articles/creating-a-new-repository

  1. Login to GitHub
  2. Click "Create a New Repo" button (top right)

51 of 97

Create the GitHub repo

Fill in the repo name

Fill in a description,

select "Public"

Click "Create Repo"

52 of 97

w00t!

Click to copy the GitHub repo URL to your clipboard

53 of 97

Tell your local Git repo about your GitHub repo

$ git remote add origin <PASTE>

For example:

$ git remote add origin git@github.com:jsquyres/kyoss-git-repository-github.git

54 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

The GitHub repo

is initially empty

Local

GitHub

55 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

Add a "remote"

to your local repo

Local

GitHub

git remote add

56 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

Now we can "push"

to GitHub!

Local

GitHub

git push

57 of 97

Push to GitHub

The first time you push, you must specify

the remote and the branch

$ git push origin master

Counting objects: 16, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (12/12), done.

Writing objects: 100% (16/16), 1.36 KiB, done.

Total 16 (delta 1), reused 0 (delta 0)

To git@github.com:jsquyres/kyoss-git-repository-github.git

* [new branch] master -> master

58 of 97

Sidenote: Git branches

This is an advanced topic; we won't cover it tonight.

Suffice it to say that the first push you do to GitHub must be:

$ git push origin master

Git will save that as the default. Subsequently, you can just do:

$ git push

59 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

After the push, the two repos are identical

Local

GitHub

git push

60 of 97

GitHub web interface

Play around on the GitHub web interface

  • See the "Files" and "Commits" tabs
  • Click on individual files
  • Click on individual commits

Everything you've done in that repo on your local disk is now up at GitHub

61 of 97

Make a local change

$ echo "bye=sad" >> goodbye.txt

$ git status

$ git diff

$ git commit -a -m "sad panda"

What just happened?

62 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

Local repo has an additional commit

Local

GitHub

63 of 97

Push that local change

$ git push

Man... that was easy, wasn't it?

64 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

After the push, GitHub also has that commit

Local

GitHub

git push

65 of 97

Edit files directly on GitHub

Click the "Files" tab

Click hello.txt

66 of 97

Edit files directly on GitHub

  1. Click the Edit button
  2. Make some changes
  3. Scroll down, type in a commit message
  4. Click the "Commit Changes" button

67 of 97

What just happened?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

GitHub now has a commit

that your local repo does not

Local

GitHub

68 of 97

How to get that change?

kyoss-git-workshop

repo

kyoss-git-workshop-github

repo

Pull from GitHub to your local repo

Local

GitHub

git pull

69 of 97

How to get that change?

$ git pull

...lots of output...

$ git show

You'll see the change you made at GitHub

70 of 97

That's all fine and good, but...

How is this useful to me?

Here's two obvious (single-user) uses:

  1. Backup your files (publicly!)
  2. Synchronize files between multiple machines

71 of 97

Synchronize your files between machines

Jeff and Alan both keep our shell startup files under version control

When we login to a new machine, we just checkout our .bashrc (etc.) files, and we get our own comfortable environment:

  • Shell prompt
  • Shell aliases
  • .vimrc, .emacs, ...etc.

72 of 97

Get a 2nd clone from GitHub

You may still have the URL to your github repository in your clipboard...

(go and copy it if you don't)

$ cd $HOME/git

$ git clone <PASTE> my-clone

What did we just do?

73 of 97

Now we have 2 clones from GitHub

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

74 of 97

Let's make a change in my-clone

$ cd $HOME/git/my-clone

$ echo yellow >> hello.txt

$ git status

$ git diff

$ git commit -a -m "hello colors"

$ git push

Go browse on GitHub and see that your commit got there

75 of 97

Pull that change down to kyoss-git-workshop

$ cd $HOME/git/kyoss-git-workshop

$ git pull

$ git show HEAD

w00t!

You have made a change in one repo and synchronized it over to your other repo

76 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

Commit a change in my-clone

77 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

git push

Push it to GitHub

78 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

git pull

Pull it to kyoss-git-workshop

79 of 97

Multiple changes

Ok, that was easy enough.

..but what if I make changes in both kyoss-git-workshop and my-clone?

80 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

Make changes in both local repos

81 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

Now try to push both back to GitHub

git push

git push

my-clone

repo

82 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

What happens?

git push

git push

?

my-clone

repo

83 of 97

There are two cases

  1. Conflicting changes
    • The two commits changed the same line(s) in the same file(s)
    • ADVANCED TOPIC / not covered tonight

  • Non-conflicting changes
    • The two commits changed different line(s) in different file(s)

84 of 97

Non-conflicting changes: 1

$ cd $HOME/git/kyoss-git-workshop

$ echo hello >> hello.txt

$ git status

$ git diff

$ git commit -a -m "hello again"

$ git push

This works fine

85 of 97

Non-conflicting changes: 2

$ cd $HOME/git/my-clone

$ echo goodbye >> goodbye.txt

$ git status

$ git diff

$ git commit -a -m "goodbye again"

$ git push

! [rejected] master -> master (non-fast forward)�error: failed to push some refs to ...

Er... what?

86 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

git push

git push

my-clone

repo

He who pushes first, wins

87 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

my-clone must pull first

git pull

88 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

This will merge in the kyoss-git-workshop change

89 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

Now my-clone can push

git push

90 of 97

What just happened?

kyoss-git-workshop-github

repo

Local

GitHub

kyoss-

git-workshop

repo

my-clone

repo

And kyoss-git-workshop can pull

git pull

91 of 97

Do it yourself

  1. Make a change in my-clone
  2. Commit and push
  3. Make a non-conflicting change in kyoss-git-workshop
  4. Commit
  5. Try to push (and see it fail)
  6. Pull to kyoss-git-workshop
  7. Run "git log --graph" to see how it all merged together
  8. Push from kyoss-git-workshop
  9. Pull to my-clone

92 of 97

Next Git Workshop Topics

That's it for the very basics.

You are still in for a lot more. Some of the very best features of Git (and some of the trickiest concepts to understand) are yet to come.

  • stashing, branching and merging
  • conflict resolution & merge tools
  • tagging
  • forking and pull requests
  • branching models (feature, dev, release)
  • deployments with git

93 of 97

Review & Recap - Git Basics

94 of 97

What Is Git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

95 of 97

Why Git?

How is it better than <insert favorite repository here>?

  • Open Source & Stable
  • Lots of Applications / Integrations
  • Everything is Local
  • Git is Small
  • Git is Fast
  • Snapshots vs. Deltas
  • Cheap Local Branching
  • The Staging Area
  • Distributed
  • Any Workflow
  • GitHub (etc)

(et

Any DVCS system is fine...

Mercurial and Bazaar are also excellent options

(Is it better than SVN/CVS?)

YES!

(e.g., Jeff Mercurial)

96 of 97

Why GitHub?

GitHub is a convenient option for hosting / sharing

any Git repository.

  • Cheap or FREE
  • Good interface for code, commits, diffs
  • Social and Collaboration integration
  • Basic issue tracking, wiki, websites

You do NOT need GitHub to use Git.

(et

97 of 97

Bonus Points

  • http://gitref.org/ - Git Reference
  • http://gitready.com/ - Git example walkthroughs, atomic
  • http://goo.gl/Pw0Fz - Git Cheat Sheet
  • tig - an excellent CLI interface to look through logs
  • git-svn - a Git core extension which facilitates integrating Git+SVN
  • git-wtf - a Git script helping identify branch sync status across multiple remotes