Git and Github

An Introduction

What We'll Cover

First Things First

Why Bother?

images/phd_final.gif

Image from PhD Comics

Better Options: Version Control

Git

images/Linus_Torvalds.jpeg

git, noun. Brit.informal. 1. an unpleasant or contemptible person.

Setting up Git

$ sudo [yum | apt ]  install git

Configs

[user]
email = wyman.lucy@gmail.com name = Lucy Wyman
[core]
editor = vim
[credential]
username = lucywyman

Using Git Locally

$ git init
# Make your changes
$ git status # or git diff
$ git add <filename>
$ git commit
$ git push origin <branchname>
$ git log

What Not To Do

http://arstechnica.com/security/2013/01/psa-dont-upload-your-important-passwords-to-github/

Git Exercise

First create a git repository!

$ mkdir my_python_app
$ cd my_python_app
$ git init

Git will do a one-time prompt for some basic information and then you have a Git Repository! All code in this code can be tracked by git as a single project.

Adding Code

Create and open a new file script.py with the following command:

def f(x):
    print(x**x)
if __name__ ==  "__main__":
    f(5)

Save this file and leave the text editor and tell git to track this code.

$ git status
$ git add script.py
$ git commit -m "My first git commit!"
$ git status
$ git push origin master
$ git log

Cloning a Repository

Git also allows you to clone a remote repository to work on another person's code. It's like downloading the entire project and it's git history.

$ cd ~
$ git clone git@github.com:DevOpsBootcamp/tinsy-flask-app.git
$ cd tinsy-flask-app
$ ls

You have successfully clone a remote repository and can start modifying the other person's code. Changes you make on your local version of this project will not affect the original version you modified (although you can push changes if you are allowed to do so by the original owner!)

Branches

Github allows you to 'branch' your codebase. This allows you to make changes on a separate track without modifying the original codebse in the same repository. Branches are preserved when you clone a remote repository.

$ git checkout broken
$ python myapp.py

Now you can see your webapp doesn't work correctly when you try to access it in the browser!

We can manually go in and fix it, or run a command to see what changed between this version and the version in the 'master' branch.

$ git diff master

Daily workflow

images/gitflow.png

Pull -> Work -> Add changes -> Commit -> Push

Larger projects have more complex workflows

GitHub!

images/octocat.jpg

Other Resources