Intro to Git and Github

What we will cover today

  • What is version control and why should we care?
  • Basics of git -- the essential commands
  • Sharing code on GitHub

What is version control?

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

Collaborate

Create anything with other people, from academic papers to entire websites and applications.

Track and revert changes

Mistakes happen. Wouldn't it be nice if you could see the changes that have been made and go 'back in time' to fix something that went wrong?

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

Rainbows and bunny rabbits!

Brief history of Version Control

1990s - CVS (Concurrent Version Systems)

2000s - SVN (Apache Subversion)

2005 - Git (well, Git)

Intro to Git

Goals of Git Design

  • Fast -- add to your team and code base quickly
  • Distributed (see slide above)
  • Each commit has a corresponding hash (track changes from everyone)
  • Everyone has a local copy of the history

Installation and Setup

Install GitHub for Desktop

Create a github account

github.com

Login with that account on GitHub for Desktop

Let's make a page on github!

Github Pages

Understanding GitHub for desktop

Add files

Changes and commits

What did we just do??

How is this all different than just saving a file?

  • When we add a new file, we tell Git to add the file to the repository to be tracked
  • When we stage an existing file (also with the keyword 'add'), we are telling Git to track the current state of our file
  • A commit saves changes made to a file, not the file as a whole. The commit will have a 'hash' so we can track which changes were committed when and by whom.

Look at our progress

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

        First commit. Added hello world to repository.
      
    

Nobody's Perfect

Undoing local changes

If you haven't committed yet

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 staged 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 --pretty=oneline
          git revert [HASH]
        
      

Look at hello_world.txt. Your changes are gone.

Nobody's Perfect

Remove a file from staging

Create new file my_new_file.txt

        
          git add my_new_file.txt
          git reset my_new_file.txt
        
      

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
      
    

Branching

  • Develop different code on the same base
  • Conduct exploratory work without affecting the work on master branch
  • Incorporate changes to your master branch only when you are ready

Branching

Create a new branch called version2

      
        git checkout -b version2
      
    

Add new lines to hello_world.txt

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

Branching

Switching branches

See all branches. Branch with * is active

      
        git branch
      
    

Switch to master and look at hello_world.txt

      
        git checkout master
      
    

Switch to version2 and look at hello_world.txt

      
        git checkout version2
      
    

Merging

Merge to get changes from one branch into another*

Switch to master and merge changes

      
        git checkout master
        git merge version2
      
    

*rebase is another option, but will not be covered in this workshop

Merging

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'
      
    

Merging

Merge conflicts, cont.

Merge from master into version2

      
        git merge master
      
    

You will be notified of a conflict. Go to the file and fix the problem. Then commit your edits.

GitHub

  • Launched in 2008
  • Leader in Social Coding
  • GitHub is a commercial site that allows users to host Git repositories publicly and privately
  • Open source projects host or mirror their repositories on GitHub
  • Post your own code for others to use or contribute to
  • Use and learn from the code in other people's repositories

GitHub

ReadME

While a README isn't a required part of a GitHub repository, it is a very good idea to have one. READMEs are a great place to describe your project or add some documentation such as how to install or use your project. You might want to include contact information - if your project becomes popular people will want to help you out.

GitHub

Get Local Repository of GitHub Repo

      
        cd ../ # Back in root directory
        mkdir hello-github
        cd hello-github
        git init
        git remote add origin git@github.com:username/NAME-OF-REPO
        git pull origin master
      
    

GitHub

Push to GitHub Repo

Edit the ReadMe file

      
        git add README
        git commit -m 'Updating readme file'
        git push origin master
      
    

Go look at your github repo online

GitHub

Pulling from remote repository

If you are working with a team, you want to make sure that you have everyone's changes before pushing your changes to the GitHub repo

      
        # Commit local changes
        git commit -m 'My latest commit'
        # Pull changes other people have made
        git pull origin master
        # Fix any conflicts (see merge conflicts above) and commit
        git commit -m 'Fixing merging conflicts'
        # Push local changes to GitHub
        git push origin master
      
    

Forking

  • There are MILLIONS of public repositories on GitHub
  • If you want to use or contribute to a repository, you can fork it.

Forking

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

Forking

Cloning

Clone to get a local repository of your fork

      
        cd ../

        git clone https://github.com/username/FORKED-REPO-NAME.git

        cd FORKED-REPO-NAME

        git remote add upstream https://github.com/original-username/FORKED-REPO-NAME.git
        #Assigns the original repository to a remote called 'upstream'

        git fetch upstream
        #Pulls in changes not present in your local repository, without modifying your files
      
    

Pull Requests

  • After you fork and clone a repository all pushed changes will go to your fork
  • These changes will not affect the original repository
  • If you would like to get your changes to be incorporated into the original repo, you can submit a pull request

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

How to manage pull requests is out of the scope of this short workshop, but you can learn more from the Github Collaborating Tutorials

Questions?

?