Git & GitHub Basics
KYOSS Presentation on Git
DVCS for everyone
By: Alan Blount & Jeff Squyres
Oct 2012
http://goo.gl/qr2JS
Why Is Version Control Important?
We all know about...
but what about this?
FAIL
FAIL
Version control is important
because "FAIL" happens
Why Is Version Control Important?
Or more likely, when working on a project
have you ever done anything like this?
Version Control to the Rescue
A Version Control System (VCS) would fix that
What is Git?
Git is an open source,
distributed version control system designed for speed and efficiency
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)
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
What is Git?
Git is an open source,
distributed version control system designed for speed and efficiency
Use it how you want to!
Enough talking -- let's do Git
G
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
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
Now on to the good stuff...
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
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!
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)
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?
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"
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"
Look at what you've done
What does status say now?
$ git status
All clear!
What exactly have we done?
Working directory
Git
repo
Git
staging area
git commit
git add
git status
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)
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
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
git status and diff
Working directory
Git
repo
Git
staging area
git status
git diff
git diff --cached
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
Time to "commit" again
Ok, let's commit our change to hello.txt
$ git commit
Or
$ git commit -m "add more to hello"
Time to "commit" again
Hey.. why didn't it work?
no changes added to commit (use "git add" and/or "git commit -a")
Add the change, then commit
Working directory
Git
repo
Git
staging area
git add
$ git add hello.txt
$ git commit
git commit
Make another change
$ echo "hello hello" >> hello.txt
$ git status
...output...
$ git diff
...output...
All that output makes sense
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
Now add, but do NOT commit
$ git add hello.txt
Remember the rule:
Run status and diff before commit!
$ git diff
$
Wait... what?
We added the change,
so diff is empty
Working directory
Git
repo
Git
staging area
git status
git diff
git add
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
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
Let's commit that
$ git add hello.txt
$ git status
$ git commit -m "I love hello"
Verify that everything is committed
$ git status
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
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
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>
One log entry
commit 21d0fddb739e2528ff03e40040d02ae7a0061272
Author: Bob Smith <bobsmith@gmail.com>
Date: Wed Oct 10 19:20:37 2012 -0400
Add hello/goodbye
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
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
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
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
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
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?
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
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
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
Do I have to use GitHub?
No!
GitHub is an excellent solution for some problems, but you can use git:
There are many alternatives to GitHub�with many feature sets.
?
Let's use GitHub!
Create a repository on GitHub
https://help.github.com/articles/creating-a-new-repository
Create the GitHub repo
Fill in the repo name
Fill in a description,
select "Public"
Click "Create Repo"
w00t!
Click to copy the GitHub repo URL to your clipboard
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
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
The GitHub repo
is initially empty
Local
GitHub
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
Add a "remote"
to your local repo
Local
GitHub
git remote add
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
Now we can "push"
to GitHub!
Local
GitHub
git push
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
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
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
After the push, the two repos are identical
Local
GitHub
git push
GitHub web interface
Play around on the GitHub web interface
Everything you've done in that repo on your local disk is now up at GitHub
Make a local change
$ echo "bye=sad" >> goodbye.txt
$ git status
$ git diff
$ git commit -a -m "sad panda"
What just happened?
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
Local repo has an additional commit
Local
GitHub
Push that local change
$ git push
Man... that was easy, wasn't it?
What just happened?
kyoss-git-workshop
repo
kyoss-git-workshop-github
repo
After the push, GitHub also has that commit
Local
GitHub
git push
Edit files directly on GitHub
Click the "Files" tab
Click hello.txt
Edit files directly on GitHub
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
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
How to get that change?
$ git pull
...lots of output...
$ git show
You'll see the change you made at GitHub
That's all fine and good, but...
How is this useful to me?
Here's two obvious (single-user) uses:
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:
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?
Now we have 2 clones from GitHub
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
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
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
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
Commit a change in my-clone
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
git push
Push it to GitHub
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
Multiple changes
Ok, that was easy enough.
..but what if I make changes in both kyoss-git-workshop and my-clone?
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
Make changes in both local repos
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
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
What happens?
git push
git push
?
my-clone
repo
There are two cases
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
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?
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
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
my-clone must pull first
git pull
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
What just happened?
kyoss-git-workshop-github
repo
Local
GitHub
kyoss-
git-workshop
repo
my-clone
repo
Now my-clone can push
git push
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
Do it yourself
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.
Review & Recap - Git Basics
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.
Why Git?
How is it better than <insert favorite repository here>?
(et
Any DVCS system is fine...
Mercurial and Bazaar are also excellent options
(Is it better than SVN/CVS?)
YES!
(e.g., Jeff Mercurial)
Why GitHub?
GitHub is a convenient option for hosting / sharing
any Git repository.
You do NOT need GitHub to use Git.
(et
Bonus Points