Intro to Git and Github

Welcome to the Git together!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

What we will cover in this workshop

  • What is version control and why should we care?
  • Basics of git -- the essential commands
  • Using GitHub
  • Github pages

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?

Version Control Types

Centralized Version Control

Examples: CVS, SVN

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

Distributed Version Control

Examples: Git, Mercurial

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

Version Control Distribution

Version control share between Bazaar, CVS, Git, Mercurial and Subversion

Intro to Git

Goals of Git Design

  • Fast -- add to your team and code base quickly
  • Distributed
  • Each commit has a corresponding hash (unique string)
  • Everyone has a local copy of the history

Git vs Github

  • Git is an open source version control system you can install locally
  • Github is a central storage space hosting repositories

Before We Start...

  • 1. Make sure you have a text editor installed

    • Atom (atom.io)
    • Sublime Text 2 (sublimetext.com/2)
    • VS Code (code.visualstudio.com/)
  • 2. Sign up for Github (github.com)

  • 3. Download the latest version of Git (git-scm.com/downloads)

Installation and Setup

Setup name and email in gitconfig

Your name will be visible on your commit history. This can be updated at any time.


$ 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
          

LET'S GIT INIT

Your first Local Repository

Go to home directory


$ cd ~/
# OR
$ cd Users\username
          

Create a "working directory"


$ mkdir my-first-repo
$ cd my-first-repo
          

Initialize repository with Git


$ git init
$ git status
          

Add files

Create a new hello_world.txt file in your new folder

Check repo status


$ git status
          

Tell Git to track our new file


$ git add hello_world.txt
$ git status
          

File is now tracked by Git

Changes and commits

Open hello_world.txt and add some more text


$ git status
          

Stage and commit the change


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

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. Commit messages should be brief but descriptive messages about what was changed. Here's a great post on writing great commit messages.

Gitflow

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 staged changes

Open hello_world.txt and add some new text


$ git add hello_world.txt
$ git reset HEAD hello_world.txt
            

Look at hello_world.txt. Your changes are gone.

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

More on using branches: https://www.atlassian.com/git/tutorials/using-branches

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
          

Branching

Branching flow

Branching

Branching flow

Branching

Branching flow

Branching

Branching flow

Branching

Branching flow - merging (spoiler)

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

Merge conflicts

Merge conflicts

Don't force a push unless you really know what you are doing

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.

            
$ 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.
            
          

Merging

Merge conflicts, cont.

            
#In our txt file:
<<<<<<< HEAD
  Hello World! This change was made on my version2 branch
=======
  Hello World! This change was made on my master branch, how exciting!
>>>>>>> master
  Let's git this together
            
          

Merging

Resovling merge conflicts

  1. Manually update the file to keep the desired changes
  2. Save your file
  3. Stage your changes
  4. Commit
            
$ git add hello_world.txt
$ git commit -m "Merged master fixed conflicts."
# Recorded resolution for 'hello_world.txt'.
# [645c4e6] Merged master fixed conflict.
            
          

Time to Git Social!

gitting social daft punk gif gitting social daft punk gif

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

Create your first repository

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

GitHub

Create your first repository

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

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.

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

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

Collaboration and Publishing to Github Pages

https://pages.github.com/

  • 1. Create a repository and setup gh pages
  • 2. Clone the repository and create your own branch
  • 3. Create a pull request for your work and merge

Questions?

?