Intro to Git and Github

by Justin James

@digitaldrummerj

http://digitaldrummerj.me

What we will cover today

  • What is version control and why should we care?
  • Basics of git -- the essential commands
  • "Gitting" social with GitHub

What is version control?

Version control allows you (and your team) to do two powerful things

 

Collaborate

Track and revert changes

Working without Version Control

Trying to make a grocery list with 5 people and no version control

The Horror!

Working with Version Control

Successfully making a grocery list with 5 people and version control

The Beauty!

What should I version control?

Anything. But not everything.

version control ALL THE THINGS!

To track or not to track

You decide what goes into version control.

You can, and should, leave some things out.

libraries, .dotfiles, api keys...

You already manage versions of your work!


Resume-September2013.docx
Resume-for-Duke-job.docx
ResumeOLD.docx
ResumeNEW.docx
ResumeREALLYREALLYNEW.docx
          
You invented your own version control!

Brief history of Version Control

1990s -- CVS (Concurrent Version Systems)

2000s -- SVN (Apache Subversion)

2005 -- Git (well, Git)

Version Control Types


Centralized Version Control

Examples: CVS, Subversion (SVN)


Distributed Version Control

Examples: Git

Centralized Version Control

one central server with each person pushing changes to the main server

One central server, each client (person) checks out and merges changes to main server

...NOT how Git works!

Distributed Version Control

each person works on her own local copy; each copy is reconciled with the main copy on the server

Each client (person) has a local repository, which they can then reconcile with the main server.

...this is how Git does it!

Why Use Git?

  • Track Changes!
  • Undo Mistakes!
  • Work Offline!
  • Collaborate On Code!

Git has its own peculiar Vocabulary

Here are some of the most basic ones.

  • repository - all the files you want to track.
  • branch - separate line of dev, with own history.
  • commit - holds information about a single change.
  • HEAD - most recent commit on current branch.

Installation and Setup

Install git - http://git-scm.com/downloads

Download latest version of Git

Installation and Setup

Setup name and email in gitconfig


$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
            

$ git config --global user.email "your_email@example.com"
# Sets the default email for git to use when you commit
            

$ git config --list
            

Setting the default text editor

By default git uses Vim as the text editor

i to enter edit mode
esc + :w or :w! to save
esc + :q or :q! to get out of Vim

instructions to change default text editor

Your first Local Repository

Go to home directory


  cd ~/ OR cd %userprofile%
            

Create a "working directory"


  mkdir first-repo
  cd first-repo
            

Initialize repository with Git


  git init
  git status
            

git status

On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Add Files

Create hello_world.txt in first-repo

Check repo status


  git status
            
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        hello_world.txt

nothing added to commit but untracked files present 
    (use "git add" to track)
    

Add Files


  git add hello_world.txt
  git status
            
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hello_world.txt
        

File is now tracked by Git

Changes and commits

Open hello_world.txt and add some more text


  git status
            
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hello_world.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   hello_world.txt

Stage and commit the change


  git add hello_world.txt
  git commit -m "First commit. Added hello world to repository."
  

[master (root-commit) d1be922] First commit. Added hello world to
             repository.
 1 file changed, 1 insertion(+)
 create mode 100644 hello_world.txt

Check Status


  git status
            

On branch master
nothing to commit, working tree clean

What did we just do?

How is this all different than just saving a file?

  • add tells Git to track file
  • commit saves files changes to Git
  • status shows file states

Look at our progress


  git log
            

  commit [HASH HERE]
  Author: Your name 
  Date:   [DATE HERE]

      First commit. Added hello world to repository.
              

Pretty Git Log


git log -–oneline –-graph -–decorate
            

* 84f0384 (HEAD -> master, origin/master) Merge pull request #2
|\
| * 925cf58 Added additional dashes on line 306
|/
* c838c82 Merge pull request #1 
|\
| * 02d2132 Updated
|/
* e20da49 initial commit
              
              

Nobody's Perfect

Undoing local changes

Open hello_world.txt and add some new text


  change hello_world.txt
  git checkout hello_world.txt
              

Look at hello_world.txt. Your changes are gone.

Nobody's Perfect

Undoing staged changes

Open hello_world.txt and add some new text


git add hello_world.txt
git reset HEAD hello_world.txt
git checkout hello_world.txt
              

Look at hello_world.txt. Your changes are gone.

Nobody's Perfect

Undoing committed changes

Open hello_world.txt and add some new text


git add hello_world.txt
git commit -am "Changing and committing some lines"
git log --oneline
git revert [HASH]
              

hello_world.txt is now same as [HASH] version

Nobody's Perfect

Delete a file

Create new file my_other_file.txt


git add my_other_file.txt
            

Manually delete your file


git rm my_other_file.txt
            

Ignoring Files

.gitignore

# Single File
config/local.js

# Directory
node_modules
bower_components

# Wildcard
*.log

Branching

Develop different code on the same base

Exploratory work without affecting master branch

Merge changes when ready to your master branch

Create Branch

Create a new branch and check it out


git checkout -b version2
            

Add new lines to hello_world.txt


git add hello_world.txt
git commit -m "Adding changes to version 2"
            

List Branches


git branch
            

master
* version2
              

The * is the active branch

Switching Branching


git checkout master
            

Switched to branch 'master'

Merging

Getting changes from one branch into another


git checkout master
git merge version2
            

Merge Conflicts

Change first line in hello_world.txt in master branch


git add hello_world.txt
git commit -m "Changing first line in master"
            

Change first line in hello_world.txt in version2 branch


git checkout version2
# open hello_world.txt and change first line
git add hello_world.txt
git commit -m "Changing first line in version2"
            

Merge Conflicts Continued

Merge from master into version2


git merge master
            

Auto-merging hello_world.txt
CONFLICT (content): Merge conflict in hello_world.txt
Automatic merge failed; fix conflicts and then commit the result.

What Does Merge Conflict Git Status Look Like?


On branch version2
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   hello_world.txt

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

What Does Merge Conflict In File Look Like?


<<<<<<< HEAD
hello world version 2
=======
hello world master changes
>>>>>>> master

            

Fixing Merge

Open file and decide what to keep


git add hello_world.txt


git commit -m "merged master"


[version2 473bce3] Merge branch 'master' into version2
  

Questions Before "Gitting" Social

Share Your Code on GitHub

github is social coding

Git + Friends = GitHub

GitHub has over 8 million users,
and over 19.2 million repositories

What is GitHub for?

  • Host Git repositories publicly and privately
  • Open source projects to host their repositories
  • Publish your code for others to use or contribute to
  • Read, copy, and learn from other's repositories
  • Contribute to open source projects

GitHub

Create your first remote repository

You will need to be logged into your GitHub account to do this.

image from GitHub help pages

GitHub

Create your first repository

How to create a new repository. Image from https://help.github.com/articles/create-a-repo

Local Repository to Remote

In First-Repo Folder


$ git remote add origin https://github.com/GITHUB-USERNAME/REPO.git
$ git push -u origin master

# -u tell Git to set a tracking reference to the remote branch
# you only need to use this flag the first time
            

Now check out your GitHub repo online!

Repository Readme

  • Not forcibly required but highly suggested
  • Describe your project and how to use
  • Describe how to install
  • Describe how to contribute to project

Get Local Copy of GitHub Repo

i.e. cloning


cd ../ # Back in root directory
git clone https://github.com/username/NAME-OF-REPO.git
cd NAME-OF-REPO/
            

Sending Changes to GitHub

Edit the ReadMe file


git add README.md
git status
git commit -m "Updating readme file"
git status
git push
git status
            

Go look at your github repo online

Get Changes from Github


# Commit local changes
git commit -m "My latest commit"

# Pull changes other people have made
git pull

# Fix any conflicts (see merge conflicts above) and commit
git commit -m "Fixing merging conflicts"

# push local changes to GitHub
git push
            

Make sure you have latest changes before pushing

Forking

Think

not

Forking

Use or Contribute to a repository.

Fork is just a copy of a repository, saved to GitHub.

Let's practice forking! How to fork a repository. Image from https://help.github.com/articles/fork-a-repo/

Get Local Copy of the Fork


$ cd ../

$ git clone https://github.com/YOUR-GITHUB-USERNAME/tiny-repo.git

$ cd tiny-repo

There are 3 Copies of this Repo

the original (on Github) => upstream
your fork (on GitHub) => origin
your clone => your local repo

Syncing

sync your local repository to the original repository


git remote add upstream https://github.com/original-user/REPO-NAME.git

git fetch upstream

git merge upstream/master

git push
            

Get Changes Into Original Repo

i.e. Pull Request (PR)

Starting a pull request

How to initiate a pull request. Image from https://help.github.com/articles/using-pull-requests

Previewing and sending pull request

How to preview and send a pull request. Image from https://help.github.com/articles/using-pull-requests

Managing pull requests

If you are the owner of repo, you will review and decide whether to merge in the pull requests.


Learn more about contributing at
Github Collaborating Tutorials.

Questions?