Git and GitHub 101
Introduction and Basics
First: Get a github3.cisco.com account!
Login with your CEC�Product: Github (NOT git!)�Category: New user request�Server: github3�Organization: CSPG/VIC
Git and GitHub: What’s the difference?
GIT
GITHUB
Subversion != Git
Do not expect Git to be exactly like Subversion.
There are fundamental differences.
Subversion
Subversion
Git
Git
Simple SVN → GIT command analogs
SVN operation | SVN commands | Git commands |
Checkout | svn checkout https://… | git clone https://github3.cisco.com/… or git clone git@github3.cisco.com://… |
See the history | svn log -r 1233:1234 svn log -c 1234 | git log git log abc123 git log --graph --decorate --abbrev-commit --pretty=oneline git show # shows the commit meta data and diff of last commit git show HEAD~1 # shows one prior to the last commit git show abc123 # shows specific commit with hash abc123 |
Add / remove files | svn add <new_files> svn del <old_files> | git add <new_files> git rm <old_files> |
See your local changes | svn status svn diff | git status git diff git diff --cached # shows what you have already staged for commit |
Delete local changes | svn clean | git checkout [<files and/or dirs>] git clean -dfx # this is the nuclear option -- be careful! |
Commit | svn commit [<files>] | git add <files with changes> git commit [<files>] git push |
Fetch remote changes | svn up | git pull [--rebase] |
Git: Storage model
Understanding Git’s storage model is�very Very VERY helpful�in understanding�how to use Git
Git: Directed acyclic graph (DAG) of commits
ae235f
bc4829
82b4f3
994c4a
master
Each blue box = one commit
Contains file tree/diff, parent pointer, author, ...etc.
“bc4829” is an abbreviation of the 40-character unique SHA1 hash
HEAD
HEAD points to the currently-checked out branch
Green box = branch name. They can move / be updated to point to new blue boxes
Git: The commit
Git: Set your name / email address
$ git config --global user.name "YOUR NAME"� $ git config --global user.email "YOUR_CEC@cisco.com"
Git: Staging area
Working�directory
Git
repo
Git
staging area�(a.k.a. “index”)
git commit
git add
git status
git diff
git diff --cached
Git: Branching
Branching is easy
and common in git!
But branching is scary!
And hard!
And slow!
Subversion branching is scary, hard, and slow
Git branching is fast and easy
Git: Branching: Exmple scenario
Git: Branching: Starting out
ae235f
bc4829
82b4f3
994c4a
master
HEAD
Git: Branching: Create a branch
ae235f
bc4829
82b4f3
994c4a
master
pr/feature
HEAD
Note that “master” is still the checked-out branch
$ git branch pr/feature
Creating a branch just means creating a label and pointing it to a node in the DAG
Git: Branching: Check out the new branch
ae235f
bc4829
82b4f3
994c4a
master
pr/feature
HEAD
Now pr/feature is the checked-out branch
$ git checkout pr/feature
Git pro tip:
git checkout -b pr/feature
both in one step
Git: Branching: Add some commits to the branch
ae235f
bc4829
82b4f3
994c4a
ab223f
master
pr/feature
cc443a
HEAD
Commits update the pr/feature branch and HEAD, but master doesn’t move
...make changes...
$ git add file1
$ git commit -m “first commit for feature branch”
...make more changes...
$ git add file2
$ git commit -m “second commit for feature branch”
Git: Branching: Uh oh...
Git: Branching: Make a new branch, add a commit
ae235f
bc4829
82b4f3
994c4a
ab223f
master
pr/feature
cc443a
pr/fix-ASAP
ce425b
HEAD
Create new branch and add a commit fixing the issue
$ git checkout master
$ git checkout -b pr/fix-ASAP
...make changes...
$ git add ...
$ git commit -m “fixes a huge problem”
Git: Branching: Let’s merge!
Git: Branching: Merge pr/fix-ASAP
ae235f
bc4829
82b4f3
994c4a
ab223f
master
pr/feature
cc443a
pr/fix-ASAP
ce425b
HEAD
Master can “fast foward” to the DAG node where pr/fix-ASAP points
$ git checkout master
$ git merge pr/fix-ASAP
master
Git: Branching: Merge pr/feature
ae235f
bc4829
82b4f3
994c4a
ab223f
master
pr/feature
cc443a
pr/fix-ASAP
ce425b
HEAD
aa993c
$ git checkout master
$ git merge pr/feature
...opens editor for merge commit message...
Merge
commit
Git: Tags
Git: Tags example
ae235f
bc4829
82b4f3
994c4a
ab223f
master
pr/feature
cc443a
pr/fix-ASAP
ce425b
HEAD
aa993c
Immovable / immutable tags.
build-342
build-343
build-341
Git: Universal help
“git help” is your friend
“git help <command>” is also your friend
Git: Additional resources
GitHub GUI client: https://desktop.github.com/
Windows people might like Git For Windows
Git: Additional resources
Git tutorials, especially for SVN users:
More tutorials:
Google for lots more Git tutorials and information
GitHub
GitHub: At Cisco
github3.cisco.com
vic organization
enic-usnic-linux repo
fnic-linux repo
esxi-enic repo
esxi-fnic repo
...
“Vic” repositories typically contain:
github3.cisco.com
jsquyres (workspace)
bturrubi (workspace)
enic-usnic-linux repo (“fork”)
enic-usnic-linux repo (“fork”)
esxi-enic repo (“fork”)
esxi-enic repo (“fork”)
Developer workspace repositories typically contain:
github3.cisco.com
jsquyres (workspace)
bturrubi (workspace)
enic-usnic-linux repo
enic-usnic-linux repo
esxi-enic repo
esxi-enic repo
vic organization
enic-usnic-linux repo
fnic-linux repo
esxi-enic repo
esxi-fnic repo
...
Developers can share and collaborate across all repos
(yes, even between workspaces)
GitHub main workflow:�GitHub Flow
GitHub flow: Make a personal branch
Team’s main development branch
Individual developer branch
GitHub flow: Developer makes some commits
GitHub flow: Developer opens a “pull request”
(i.e., request to “pull” the code from the developer branch into the main line)
GitHub flow: Discuss and review the code
GitHub flow: Merge the code back to the main line
GiHub flow: Let’s do that IRL
Fork me!
GiHub flow: Example scenario
Do this on GitHub3
Do this with Git
Do this with Git
Do this with Git
Do this with Git
Do this on GitHub3
Do this on GitHub3
Do this with an editor
Do this on GitHub3
Do this on GitHub3
Live demonstration
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example repo
vic/git-example
repo
fork
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example repo
git-example�repo
Clone
vic/git-example
repo
NOTE
In the Webex recording of these slides, we discussed cloning your fork repository.
These slides have been changed slightly to show you cloning the VIC repository and adding a “remote” to communicate with your fork.
The concept of “remote” will be discussed in the “102” slides.
If you’re new to Git / GitHub, use the “clone the VIC repository” method that is now shown in both the 101 and 102 slides. As you get more familiar with Git / GitHub and get more sophisticated in your Git usage, you may choose to use the “clone the fork” method if you wish.
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example repo
git-example�repo
Clone
vic/git-example
repo
$ git clone git@github3.cisco.com:vic/git-example.git
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example repo
git-example�repo
Setup remote
vic/git-example
repo
$ git remote add myfork \
git@github3.cisco.com:jsquyres/git-example.git
Take this command on faith for the moment; we’ll explain it in the 102 class
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example repo
git-example�repo
Prevent accidentally pushing to the VIC repo
vic/git-example
repo
$ git remote set-url origin bogus --push
Take this command on faith for the moment; we’ll explain it in the 102 class
GitHub flow: What we just did
Developer machine (laptop, server, etc.)
git-example�repo
$ git checkout -b pr/my-local-branch
GitHub flow: What we just did
Developer machine (laptop, server, etc.)
git-example�repo
$ vi file1 file2 file3 …
$ git add file1 file2 file3 …
$ git commit
GitHub flow: What we just did
github3.cisco.com
Developer machine (laptop, server, etc.)
jsquyres/git-example
Git-example repo
Push
vic/git-example
repo
$ git push myfork --dry-run
# If the “dry run” looks ok, do it For Real
$ git push myfork
GitHub flow: What we just did
github3.cisco.com
jsquyres/git-example
vic/git-example
repo
PR
GitHub flow: What we just did
github3.cisco.com
jsquyres/git-example
vic/git-example
repo
PR
GitHub flow: What we just did
github3.cisco.com
vic/git-example
GitHub: Additional resources
Lots and lots of Git / Github step-by-step guides:
Google for lots more GitHub information
Additional resources
Our favorite (basic) Git config
$ cat $HOME/.gitconfig�…�[core]� # set your favorite editor if it doesn’t come up by default� editor = vim�[alias]� pr = pull --rebase� graph = log --graph --decorate --abbrev-commit --pretty=oneline� grapha = log --graph --decorate --abbrev-commit --pretty=oneline --all� unstage = reset HEAD --� cached = diff --cached� last = log -1 --stat�[color "status"]� added = green� changed = red� untracked = magenta
Command line interaction with GitHub
Example: checkout a pull request:
$ cd my_libfabric_git_clone�$ hub checkout https://github.com/ofiwg/libfabric/pull/2582
Get it:
Open questions / discussion