Hey Early Birds!

Get a jump on the day and install git now!

http://git-scm.com/downloads

Download latest version of Git

Intro to Git Workshop

Slides: https://girldevelopit.github.io/gdi-git-workshop

Welcome!

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 Today

  • What is version control?
  • Basics of git – the essential commands
  • "Gitting" social with Github

Real Talk

Git isn't simple. Like most powerful tools, it's complex and takes some time to master.

What Is Version Control?

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

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?

Collaborate

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

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)

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.

More info about the differences here.

Intro To Git

Goals of Git Design

  • Fast -- add to your team and code base quickly
  • Distributed (see slide above) - you can work without a network connection
  • Each commit has a unique identifier, a SHA (Secure Hash Algorithm, a.k.a hash)
  • Everyone has a local copy of the history

Installation And Setup

Download latest version of Git

On Mac

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

On Windows

Install Git for Windows https://git-for-windows.github.io/



Open The Terminal

On Mac

Press CMD+SPACE
and search 'Terminal' and hit enter

On Windows

Press WinKey
and search 'mintty' and hit enter

Installation And Setup

Configure Git with your name and email


# I'm a comment! Don't enter me in command line. 
# Do enter lines that start with a $, but don't type the $. 

# Set the name git associates with your commits
$ git config --global user.name "Your Name Here"
          
# Set the email git associates with your commits
$ git config --global user.email "your_email@example.com"
          
# Show your configuration settings
$ git config --list
          

Installation And Setup

An SSH key is like a secret handshake between your computer and Github. It allows Github's servers to verify your identity via public-key cryptography and challenge-response authentication- not via you typing in your Github password. It's super secure and not too difficult to set up.

More info about SSH keys.

Installation And Setup

Setup ssh keys following this guide:
https://help.github.com/articles/generating-ssh-keys

Your First Local Repository


# Change to your home directory
cd ~

# OR
cd

# Create and move to a "working directory"
mkdir my-repo
cd my-repo

# Initialize directory as a Git repository
git init
          

Add Files

Create a new hello.txt file in your my-repo directory, then check repo status


touch hello.txt
git status
          

Tell Git to track our new file


git add hello.txt
git status
          

File is now tracked by Git!

Changes And Commits

Open hello.txt and add some text, then check your git status


git status
          

Stage and commit the change


git add hello.txt
git commit -m "Added first file to repository."
          

What Did We Just Do?

How is this all different than just saving a file?

  • When we add a file, we tell Git to track the current state of it. This is also known as 'staging' the file.
  • A commit saves changes made to staged files, not the whole files. The commit allows us to track which changes were committed when and by whom.

Look At Our Progress


git log

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

      Added first file to repository.

            

Cool! We'll be using that SHA later...

Git Is Helpful

Always read the output of running git commands. They may look a little intimidating, but they contain all sorts of useful information. When in doubt, run:


git status
        

Basic Git Worklow

This is the general workflow in a Git repository


git init            # Just once, to initialize
      # Do some stuff to files
git status          # What's up, git?
git add [filename]  # Stage changes to a file
git commit -m ""    # Commit a set of changes
git status          # What's up, git?
          

Undoing Changes

Undoing Local Changes

If you've added some new text to hello.txt, but not yet staged it, just run:


git checkout hello.txt 
            

hello.txt is reset to its state at the last commit - your changes are gone!

Undoing Changes

Un-stage a file

If you already staged hello.txt, run:


git reset hello.txt 
            

hello.txt is no longer staged, you can make whatever changes you like to it, and re-stage when it's ready.

Undoing Changes

Delete A Staged File

If you've already staged hello.txt, but want to delete it instead, run:


git rm -f hello.txt
          

The file is deleted, and its deletion will be part of your commit.

Undoing Changes

Undo staged changes

If you've made some changes to hello.txt and staged it, but want the changes to disappear, run:


git reset hello.txt     # Unstage
git checkout hello.txt  # Undo all my local changes
            

hello.txt is unstaged and reset to its state at the last commit - your changes are gone!

Undoing Changes

Undoing committed changes: Part 1

If you've made changes and committed them, you can revert all the changes that happened in a specific commit by running:


git log           # Display log of commits
git revert [SHA]  # Un-does specified commit with opposite changes
            

This doesn't have to be your most recent commit- you can revert any commit. It will just undo the entire set of changes that were included in that commit.

Undoing Changes

Undoing Committed Changes: Part 2

You can roll back your repo's state to where it was after any previous commit by running this with the SHA of the commit you'd like to reset to:


git log                  # Display log of commits
git reset --hard [SHA]   # Reset the repo
            

This rolls back all changes since the specified commit - be careful! This isn't often used in real life.

Branching

Provide you with a separate space for you make changes. It's part of how you collaborate with other people.

  • Focus on small changes
  • New features
  • Improve existing code
  • Hot Fixes — EMERGENCY!

Merging

Combine your current branch with another branch

  • Part of how you collaborate with other people
  • There are different ways to merge

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.

GitHub

Create Your First Repository

How to create a new repository.

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

Setting Up Remote Connection

Copy SSH link from Github repo

link to clone repo

# From inside your local git repo
git remote add origin [pasted link from Github]

# Pull first, when creating connection
git pull origin master  

# Push your local work to the repo
git push origin master  
          

GitHub

Push to GitHub Repo

As you make changes locally, push to Github.

Make a change to your README file, then add and commit it. Then, push!


git push origin master
          

Go check out your Github repo online to see your changes.

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


# Pull current state from Github
git pull origin master  

# Fix any conflicts (see merge conflicts above) and commit
# then, 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.
  • This will create a copy of it in your Github account

Forking

Go to https://github.com/gdiseattle/GitResources and hit the fork button.

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

Forking

Cloning

Go to your GitResources repo on Github, find this link and copy it.

Link to clone repo

Forking

Cloning

Navigate to the location you want new directory to be in- NOT INSIDE ANOTHER GIT REPO! Then, clone to get a local repository of your fork.


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

cd FORKED-REPO-NAME
          

Cloning

The git clone command does a lot!

  • Creates a local directory for the repo
  • Initializes that directory as a Git repo
  • Adds a remote connection called origin to the Github repo it was cloned from
  • Pulls from the Github repo so it's perfectly up-to-date

Cloning

Add a remote connection to the original repository, so you can stay up to date with their changes:


# Creates a remote connection, called "upstream",
#   to the original repo on Github
git remote add upstream https://github.com/original-username/FORKED-REPO-NAME.git

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

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

Pull Requests

  • Go to your GitResources directory
  • Add a resource to the list
  • Save, add, and commit your changes
  • Push your changes to your Github repo

git add .
git commit -m "Add great new resource"
git push origin master
          

Starting A Pull Request

Visit your Github repo page

Go to Pull Requests. Start a new Pull Request.

Previewing And Sending Pull Request

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

Fill out the form with a polite, helpful description, and submit.

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?

?

Thanks!

Cheri Allen

@cherimarie