Skip to content

GitHub Intro

Daniel Blazevski edited this page Sep 1, 2016 · 7 revisions

DEV - GitHub and Git

Table of Contents

1 Get a GitHub account

2 Install Git

2.1 MacOSX

2.2 Linux - Ubuntu

3 Configuring Git

4 Create a Repo

5 Commit a Push

6 Fork our Repo

7 Issue a Pull Request

8 Creating a new branch

9 Merging the branches

10 Return to a previous version

In this Developmental Setup we’ll be setting up Github and getting used to basic features of Git. You may be able to skip many parts of this - feel free to help others!

## Get a GitHub account

Go to github.com and sign up for an account if you don’t already have one (choose the free option and there’s no need to set up an organization. We’ll be using this account throughout the session, and you’ll eventually show the project repos to Mentor Companies, so make sure everything is presentable (e.g. appropriate account names).

## Install Git ### MacOSX On OSX 10.9 and above, Git should already be installed. You can check by opening up a Terminal and typing:
$ git version

If you don’t have Git, you can install it as part of the XCode or directly from the Git page on SourceForge. For the SourceForge package, just click thru the directions or refer to the README. If you run into a warning about security, you can get around this by going to System Preferences, then Security & Privacy.

### Linux - Ubuntu You should be able to install git thru the apt-get package manager. Make sure it’s updated by opening up a Terminal and entering: ``` $ sudo apt-get update $ sudo apt-get upgrade ``` Then installing with: ``` $ sudo apt-get install git ``` > Pro-tip: Don’t forget the apt-cache search command when you don’t know the exact name of the package you want, > and use yum on Fedora systems. ## Configuring Git It’s nice to have a credential helper so you only need to enter your Github password once when using a repo. You can configure this pretty easily by following the directions [here](https://www.google.com/url?q=https%3A%2F%2Fhelp.github.com%2Farticles%2Fcaching-your-github-password-in-git%2F&sa=D&sntz=1&usg=AFQjCNHkrrY0v-Qcx13TUwOvEUhZ06vOYw).

Also, you can setup your Git Workspace by following the directions here which would give more context in navigating through a repo and also make your terminal look better. Below is an example of how the Git workspace (and terminal) would look before and after the configuration.

Before

After

Pro-tip: If you don’t know where a program (like Git for example) is installed, simply type $ which git in the Terminal (without the $, of course).

## Create a Repo

Start by creating a repo from the Github website. This will just be an example repo that you can delete later if you want. When you’re creating it, be sure to initialize a README.md file, but don’t worry about a .gitignore or license for now.

Once the repo has been created on Github, clone it by copying the HTTPS clone url in the bottom right of the repo page to your clipboard

Decide which directory you want to clone your repo into and change to that directory from your Terminal. I personally prefer cloning into my HOME directory so I use:

$ cd ~

Now clone it with the git clone command:

$ git clone <paste your clone URL here>

If you use the ls (with the -a option for hidden files) you should now see a directory with a README.md file and a hidden .git directory (plus the standard . and .. aliases for the current and parent directories). The .git directory contains all the files used for version control, feel free to explore it, but be careful not to alter any files.

## Commit a Push

Now we can edit the README markdown and push the changes to our repo. Use your favorite editor (I like Sublime Text 2) to open README.md in your newly cloned directory. Make the README a little nicer by updating the title, and adding a few of the features seen here. For example, I added a link to the Insight DE page by adding the following line:

[Insight Data Engineering Homepage](http://www.insightdataengineering.com)

Once you’re happy with your Markdown, cd into your cloned directory and add the changes to the push with:

$ git add README.md

You can use the git status command to see what changes are staged and ready to be committed:

$ git status

Let’s make one more change by creating a csv (comma separated value) called test.csv the that directory using your favorite editor with the following text:

1, 2, 3
4, 5, 6

then add it to the push using:

$ git add test.csv

You can use git status again to see that test.csv has been added to staging. Now let’s commit this with a message using:

$ git commit -m “first commit”

You’ll have to include a message with each commit, usually detailing the changes you made. The last step is to link the cloned repo to the remote GitHub repo with the command:

$ git remote add master <paste your HTTPS URL>

which adds it to the master branch.

If you use git status, it will be empty “nothing to commit, working directory clean”, but it will say “Your branch is ahead of 'origin/master' by 1 commit.”

You’re now ready to push with:

$ git push

You may be prompted for a username and password depending on your configuration, but you should only have to do this once. You should also only have to enter the above command once per branch. Now refresh the Github repo to see the changes you’ve made.

You can also see what changes you’ve made to your GitHub repo using

$ git log --oneline --graph --all --decorate

You can also see that if you use git status, the staging area is still empty, but now says, “Your branch is up-to-date with 'origin/master'.” since we just pushed.

There are times where you don’t want to commit data, perhaps because it’s too large or is not public. You can use the .gitignore file to omit certain extensions or files, which we’ll create in the cloned repo with:

$ touch .gitignore

Then open it and add this line:

*.csv

Then add, commit, and push with:

$ git add .gitignore
$ git commit -m ‘ignore csv files’
$ git push

If you check your GitHub repo, you’ll still see the csv file because the .gitignore only ignore future commits. Let’s remove it with:

$ git rm test.csv
$ git commit -m ‘removed test.csv’
$ git push

Now it should be gone. Let’s edit both files a little bit.

  1. Change the test.csv to be
1, 2, 3
4, 5, 6
‘yo’, ‘yo’, ‘yo’
  1. Change the README.md to be
[Insight Data Engineering Homepage](http://www.insightdataengineering.com) test

Now, if you try to commit all the files in your cloned repo with:

$ git add .
$ git commit -m “testing my ignore file”

(Note: you could use git add -A instead of git add .) Git will add only the README.md file.

## Fork our Repo

Now go to this repo on Github. You could simply clone the directory by copying the HTTP URL and entering:

$ git clone https://github.com/InsightDataScience/fork-example.git

but then you wouldn’t be able to push any changes since you don’t own this. Instead fork the directory from Github by clicking the forking button in the top right of my repo.

After a few seconds, you can refresh and should see that repo has been transferred to you. You can now clone it on to your machine by copying the HTTPS URL and using the command (make sure you cd into whatever directory you want to clone into first) :

$ git clone <insert the HTTPS URL for your forked repo>
## Issue a Pull Request

Now edit the README markdown in the cloned directory on your local system and add a line at the end with your name and Github username, like below:

David Drummond - ddrum001

then add, commit, and push:

$ git add README.md $ git commit -m ‘added my name to the readme’ $ git push

You can now go to Github and issue a pull request to have your changes incorporated in the main repo. Go back to https://github.com/InsightDataScience/fork-example.git and click “Pull Requests” on the right menu:

Click “New pull request”

Click on “compare across forks”.

Select the fork that you want to become to new base fork.

Click on “Create pull request”. You’ll be directed to a new page where you can edit some comments. Click on “Create pull request”. It will be on the right side.

Congratulations, you’ve created a pull request, which the owner of the base fork (ddrum001) will be able to approve. The code changes will then be incorporated into the the base fork ddrum001/fork-example.

## Creating a new branch

When you are making updates to your code, you may not know if it’s going to work out or you may want to go try several different things. You may not want to make those changes to the master branch. Rather, you can create a new branch and experiment on that branch. So, let’s create a new branch, where we can make new edits.

Create a new branch:

$ git branch add-excitement
$ git checkout add-excitement

We’ve now created a new branch and moved to using that new branch. If you use

$ git log --oneline --graph --all --decorate

you should see that your HEAD is now on the new branch add-excitement, which is different from the master branch.

Add another file called your README-Crazy.md:

Yay! New branch! This is so freaking exciting!!!! NEW BRANCH!

You can add

$ git add .

and commit

$ git commit -m “new branch: first edit (with more excitement :)  !!!)”

Let’s push it with

$ git push -u origin add-excitement

Now, if you go to Github, you can see the 2 branches in the “branch” menu and you can select the branch that you’d like to see on the website: You’ll see different files on the two different branches now.

## Merging the branches

Let’s move back to the master branch:

$ git checkout master

Merging the changes with add-excitement is a simple command:

$ git merge add-excitement

Use the git log --oneline --graph --all --decorate command and you’ll see that the two branches have merged and you are at the HEAD again.

If you go to Github, you’ll see that the master and add-excitement branches are still different. That’s because we haven’t pushed these changes to the master yet. Let’s do that.

$ git push

You can that the code has changed on Github too now.

## Return to a previous version

Use git log --oneline --graph --all --decorate to find the commit hash for the version that you want to return to.

Checkout the commit that you want using the commit hash:

git checkout <commit-hash>  

Then, create a new branch. Let’s call it “reverting”:

git checkout -b reverting

If you do ls here, you’ll see only the README.md and test.csv files. If you switch back to the master branch and do ls, you'll see that we have README.md, test.csv, and README-Crazy.md.

Clone this wiki locally