Intro to Git and Github

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

Thanks to Velir for Sponsoring Us!

velir logo

Welcome!

Tell us about yourself.

  • What's your name?
  • What do you hope to get out of the class?
  • What's your OS? MAC, Linux, PC?

What we will cover today

  • Intro to the Command-line Interface (CLI)
  • What is version control and why should we care?
  • What is git: a non-technical explanation
  • Prepare for next week: install git, connect with GitHub

What we will cover next week

  • Essential git commands
  • Branches & merging
  • Start a git repo, send it to GitHub
  • Fork a repo from GitHub

What is the Command-line Interface?

  • a user interface application to a computer's operating system
  • a.k.a console user interface, character user interface (CUI), and Terminal.
  • primary means of interaction with most computer systems until the introduction of the video display terminal in the mid-1960s
  • The interface is usually implemented with a command line shell, which is a program that accepts commands as text input and converts commands to appropriate operating system functions.

Why Use The Command Line?

(Spoiler alert: We're gonna use it for git). But also:

  • Create, find, edit* files & folders
  • Run and manage programs and databases
  • Do the above remotely.
  • Do it all with speed & power (example: make your own aliases, aka shortcuts).

Different Flavors of Command Line

Most basic commands are shared across platforms

Mac & Linux

  • Both use BASH
  • "Terminal" is the default CLI in Mac

PC running Windows

  • "Command Prompt Program" is the default Windows CLI
  • Quick way to open -> Start menu, search for "cmd"
  • We're going to use the git bash for the lab

Customize your Command-line

You can improve your command line experience by using alternative CLI apps.

Basic Commands

  • pwd: print working directory. Tells you where you are!
    • use cd on windows
  • ls, ls-a, ls-al: list the files in the folder (see hidden files, see permissions)
    • use DIR on windows
  • cd: short for change directory. Lets you move around!
  • mkdir: make a directory. Ex "mkdir my_file"
  • touch: make a file. Ex "touch my_file"
  • cat: read file in CLI or move contents of one file into another/new file.
  • Edit a file with another program. **
    Ex. "vim myfile"; OR:
    • "Open -e myfile" opens in Textedit
    • "notepad.exe myfile" on windows"

About Vim

(A common default text editor)

  • Hit "i" or "I" to start writing in the document
  • Once you are done inserting text: hit "Esc"
  • Type ":w" to save, then ":q" to quit the vim screen (or just ":wq")
  • To close without saving, type ":q!"
  • Cheat sheet

Command Line Practice

  • Make a new folder
  • Go into it
  • Create a file (with vim or another text editor)
  • Move back up to the initial directory

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!

Working without Version Control, example 2

A more clear, less fun example.

files without version control, can't
            access older versions

The Horror!

Working with Version Control, example 2

A more clear, less fun example.

files with version control, can get back to older versions

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.

Version Control Distribution

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

Stats from April 2014

Version Control Distribution Change

Growth of decentralized version control (git, mercurial) versus centralized (subversion, CVS)

Dark blue section = decentralized version control, which includes git
Light blue = centralized, marked primarily by subversion.

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

What Git Does

  • Keeps track of everything in your project
  • Alerts you to conflicts when you merge one version of the project with another
  • Allows you to jump back into an earlier version
  • Lets you easily make copies ("branches") of the project that you can track and merge

Git is not the same as GitHub

  • Git is software you install on your computer or server
  • GitHub is a web-based repository that hosts projects and files managed by Git.
  • You connect and authenticate to GitHub via your computer
  • More about GitHub later . . . .

First Steps to Using Git

  • Start the repository with "git init": You tell git that you're starting a repository. (Not needed if you're working on a project from github/other git repository). And then you take snapshots of it by a two step proccess: git add and git commit
  • As you make changes, commit them.

What is a Commit?

  • Commit = snapshot of all the files as they look at that point in time
  • If you make a mistake, you can go back to an earlier commit
  • Each commit is identifed by a unique number and letter combination, called a hash.

Example of past commits

git commit example from git log

Making Commits

  • Making a commit is a two step process: add and commit
  • stage the commit by adding the files: git add.
  • take the actual commit by running: git commit and leaving a note.
  • Example:
    git add .
    git commit -m "change menu link for contact page"

Use Git branches to write and test out code safely

  • When you create a new branch of a project, all the project files are instantly copied into a new version
branch example
  • You can do whatever you want to this new branch without altering the existing project

Using branches: example

branch example

Using branches: example

branch example
  • If the code in the new branch works, we can merge it into the master branch
  • If the code doesn't work, we can just delete that branch and try again.

First steps to using git, a summary

  • Have git installed on your computer
  • Initialize a project in git
  • Make commits as you add to and change your project
  • Use branches to work on experimental code
  • To share, push to a remote repository, like GitHub

Git makes sharing your projects easy

  • Commits and branching make for safer collaboration
  • Someone can clone or fork your project or repository and work on it - how easily the changes are merged depends on how your project and team is organized. This is also where github makes things easier.
  • Github makes collaborating on, and contributing to, git repositories easier
  • Fork and git clone are used to start collaborating on projects

More about 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

Installation and Set up

  • To work with Git on your computer:
    • Install git on your computer
    • Do some initial configurations on git
  • To push and pull files from GitHub:
    • Make an account on github
    • Make an SSH key on your computer and add it to your GitHub account

Installation and Setup

Install git

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
            

Installation and Setup

Setup ssh keys


             $ cd ~/.ssh
            

$ ssh-keygen -t rsa -C "your_email@example.com"
# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
            

Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
            

Installation and Setup

Get SSH Key


Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
            

Installation and Setup

Copy the SSH Key to your clipboard


# PC:              
$ clip < ~/.ssh/id_rsa.pub 

# Mac:
$ pbcopy < ~/.ssh/id_rsa.pub

            

If "clip" doesn't work on PC, try "cat ~/.ssh/id_rsa.pub"

Add SSH Key to Github

Steps to add SSH key to github account

Installation and Setup

Test your connection to GitHub from your computer


# ssh -T git@github.com
          

You should see:


# Hi (github user name)! 
# You've successfully authenticated, but GitHub does not provide shell access.
            

Second Class

  • Start a local repository
  • Create a branch, merge it
  • Start a GH repository and push local to it
  • If time, fork, clone & contribute to "Boston Other History" repo/map

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 files, we tell Git to add the file to the repository to be tracked:
    • this applies to a new file AND to any changes made to an existing 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.
              

Look at our progress, visually


  gitk
            
Image of commits for this curriculum repo

Nobody's Perfect

Undoing local changes

If you haven't committed yet

Open hello_world.txt and add some new text

after making change to 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 --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
(Use rm filename or delete through the GUI)


git rm my_other_file.txt
            

Git diff

Make a change to your hello_world file

Before you add the changes, run:


git diff
              

To see differences between commits:


git diff oneSHA anotherSHA
            

To see differences between branches:


git diff one_branch...another_branch
            

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"
            

Make sure to commit your changes!

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

We'll work with two GitHub repositories

  1. Create a brand new repository on GitHub and clone it to your desktop
  2. Go back to the inital repository we just created and push that to GitHub. Do this by:
    • create a placeholder repository on GitHub
    • run a quick configuration command.

GitHub: 1st Exercise

Create a new repository

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

GitHub

Create a new repository

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

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

  • Copy the URL of your GitHub Repository to your clipboard
  • Where to find the git clone URL in GitHub
  • Then, back on your command line:

cd ../ # Back in root directory
git clone (SSH url) 
            

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

  • To simulate this, make a change to a file in your project within GitHub.

GitHub

Pulling from remote repository

Now back on your commandline, pull those changes down from GitHub


# Pull changes other people have made
git pull origin master
            

You'll frequently need to deal with merge conflicts when doing this.

GitHub: 2nd Exercise

Moving a local git repository to GitHub for the first time

  1. Make another repository on GitHub.
  2. Copy the URL for the repository
  3. Back on your computer, go to your first repository, and run:
  4. 
    git remote add origin (address of github repo)
    git push origin master
                

    Note: if you added a README or gitignore file on GitHub you'll have to merge those before you can push up your repository

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
            

More about syncing your local repo with the initial forked project.

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

Class Project!

  • Fork the repository at https://github.com/katebron/other-boston-history
  • Clone your version of the repository to your local environment
  • Edit the boston_hx.geojson or resources.txt files - or add your own file!
  • Push this up to your version of the repository
  • Issue a pull request at my github page of the projet

Resources

Git

Command line