git

Getting Started with Git & GitHub

Git

Once upon a time Linus Torvalds and his friends had a problem. The team that created the Linux operating system needed to manage the source code of Linux's kernel - the very core of the OS. Managing a large codebase requires speed, the ability to work on many parts of the code simultaneously, the ability to work on code without being connected to a central server, and a simple design. Git was created for this purpose.

Why Version Control?

"Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later."

When writing software (or any document) we are constantly making changes and saving them. Version control software like Git is a means of managing this process in a reliable and efficient way. The usefulness of version control becomes even more apparent when collaborating on projects with others.

Setting Up Git

First, install Git.

Now fire up Terminal if you're on Mac/Linux or Git Bash if you're on Windows. You will need to configure some settings first. Make sure that the email address you use in this step is the same email address you use when you make your GitHub account later on.

~$ git config --global user.name "Jeff Leek"
~$ git config --global user.email "jeff@example.com"

Your First Repository

A Git repository is just a directory where you actively track the changes to the files in the directory using Git. Let's create a new directory, cd into it, and then initialize Git.

~$ mkdir my_first_repo
~$ cd my_first_repo
~$ git init


Now that you've initialized git we can add our first file to the repository. I'm going to combine echo with output redirection (>) in order to create a simple text file. Then I'm going to use the cat command in order to view the contents of the file I created.

~$ echo git is fun! > file.txt
~$ cat file.txt
git is fun!


We need to tell Git to track changes made to our new file. We can do this with the git add command. Then we need to confirm the changes we've made to the file with git commit. The -m flag and the following message represents the commit message where you should leave a short note about what changes you made to the files in your repository for this commit.

~$ git add file.txt
~$ git commit -m "first commit"


Every time you change a file in a git repository, you need to git add the file. You can imagine that some repositories have thousands of files and going through to add every file isn't practical. You can use git add -A to git add every file in a directory, however there may be some files that you don't want to add, for example hidden files or files that contain personal information like API keys. In order to ignore certain files, you can create a .gitignore file in the top level of your Git repository. If you don't want a file to be tracked just include its name in .gitignore. GitHub has a great repository of commonly used .gitignore files. Let's create a .gitignore for this repository.

~$ echo .DS_Store > .gitignore
~$ git add -A
~$ git commit -m "added .gitignore"

GitHub

Go to GitHub and create an account. Make sure you use the same email address you used when you configured Git. Select the free account.

GitHub is a free remote server for Git repositories and it's a center for collaborative software projects online. You can use your GitHub account as a portfolio to showcase your personal projects and your contributions to others projects.

Now that you've got your account set up, let's create your first repository. After you log into GitHub click on the plus sign in the upper right-hand corner of the page. Select "New Repository." Name your repository my_first_repo, make sure it's a public repository, and click "create repository." You've just created your first repository on GitHub! Now, let's link your GitHub repository to your local Git repository (also called my_first_repo).

On the command line you should still be in your my_first_repo directory, but if you strayed for some reason cd back into my_first_repo. We're now going to link this repository with GitHub. In the following script make sure to replace YOUR_USERNAME with your GitHub username. At some point in this process you may be asked for your username or password, in which case you should provide your GitHub credentials.

~$ git remote add origin https://github.com/YOUR_USERNAME/my_first_repo.git
~$ git push -u origin master


Refresh the webpage of your GitHub repository and voilá! Your local Git repository is now on GitHub! After making more changes to the local copy of your git repository, just use git push in order to put your commits on GitHub.

Forking, Cloning, and Pull Requests

The most important feature of GitHub is the ability to collaborate on software projects. Most collaboration on GitHub follows the following formula:

  1. Fork a repository.
    • Forking a repo creates your own personal copy of another GitHub user's repository. You can modify your fork without affecting their repository.
  2. Clone your fork onto your computer so that you can modify the code.

  3. Make your changes to the code. git add, git commit, and git push your changes.

  4. Issue a pull request.
    • When you issue a pull request you are asking the owner of the repository which you forked to incorporate your changes into their repository.

Repository owners will review and possibly comment on the changes in your pull request. If they approve the modifications you've made, they'll merge your pull request into their repository. Pull requests are a democratizing force in the world of open source software. Anyone with a GitHub account can improve or create features for major tools like Ruby on Rails, Bitcoin, or jquery.

Let's walk through the process of GitHub collaboration from the command line perspective. First, fork the octocat/Spoon-Knife repository. This repository was created by GitHub just for practicing forking and pull requests. You can fork this repository by clicking "Fork" in the upper-right hand corner of the repository's page.

After clicking "Fork", an animation will appear while GitHub creates your personal copy of octocat/Spoon-Knife. After this animation, you should be brought to your newly created fork! On the right hand side of the page beneath "Settings" there is a clipboard with an arrow what says "Copy to clipboard" when you hover your mouse over it. Click the "Copy to clipboard" button.

Now pull up your command line interface and cd to wherever you want this to keep this repository. After you've found a good spot we're going to git clone this repository, which will copy the contents of your fork onto your local machine. You can then cd into the copied repository.

~$ git clone https://github.com/YOUR_USERNAME/Spoon-Knife.git
~$ cd Spoon-Knife/


We're going to add a new file to this repository and then we'll submit a pull request. First we'll create a new markdown file, then we'll git add and git commit our changes. Finally we'll git push our changes so our fork on GitHub matches our local repository.

~$ echo My first pull request > NEWS.md
~$ git add NEWS.md
~$ git commit -m "added file NEWS.md"
~$ git push


Take a look at your fork on GitHub, the file NEWS.md should have appeared. Now that you've made your modifications to Spoon-Knife you can send a pull request! Click the green "Review" button on the left side of your repository page. Review your changes to Spoon-Knife and then click "Create pull request." Give your pull request a title and a description, then click "Create pull request" a second time. Congratulations! You've just made your first pull request. Unfortunately pull requests are not accepted in this repository, but many GitHub project welcome pull requests!

Exercises

Using your new Git & GitHub skills, try each exercise below. You can reveal our answers to each exercise by clicking the blue box. Do not change your working directory or take any other action without being explicitly instructed to do so. You should however feel free to fool around with Git just to experiment and to get more practice!


  1. Create a folder called ex_repo, make it your working directory, and initialize a Git repository.
~$ mkdir ex_repo
~$ cd ex_repo
~$ git init

  1. Create a new file called file.txt, add file.txt to the files that are being tracked in the repository, and commit your changes to the repository.
~$ touch file.txt
~$ git add file.txt
~$ git commit -m "added file.txt"

Resources