Git

Posted by Jeff Bayes on July 10, 2015

Note: this post is migrated from the last blog. Its post date was February 2, 2015. Header paragraph edited to reflect that.

This is the transcribed version of a talk I gave to the UO Web Development Club last Winter. Our school introduces students to Git very late in the game, and we (the club) thought it was important enough to introduce our members to it early, and help dispel any confusion with what it is and how it works.

If you’d like, feel free to check out the slideshow I made that accompanied the talk.


Bear with me on this analogy; let’s think about coding on a project like playing a video game. Not just any video game, but a particularly hard one - one where you’re liable to die at any time and be sent back to the beginning. You’d be crazy not to save the game, especially before big boss fights.


Matrix Morpheus on Git


Git is your save button on your coding adventure, so let’s start talking about how to get started with Git. First, you’re going to want to install it on your computer if you don’t have it. The official Git Documentation has plenty on that, so if you don’t have it then go ahead and install it.

Now that we all have Git installed, the first step is to initialize a Git repository for your project. Using your terminal, navigate to the root folder of the project you want Git to track. Now, the magic words:

git init


Tada! You now have an empty Git repository in your project directory, but just like a game save file, it doesn’t do you any good if it’s empty. Let’s give it something to track with this:

git add -A


This command (or git add –all) will look for everything in all subfolders of the directory you’re in, and stage any changes you’ve made since your last git add. Since this is the first time you’ve run git add, it will just add every file in its current state as ready to be commmitted to the Git repository. In this case, “git add” is the base command, and what comes after it determines exactly what it does. Here are some other handy ways to use it:

git add ../app.js   // This only adds the single file you designate.
git add .           // This is similar to -A, but does not remove files if you've deleted them.
git add -u          // Opposite of above, this ONLY removes deleted files.


Finally, the holy grail, our save button - git commit. This command takes everything that has been staged by git add and creates a save point. This point, so long as you have your git repository, will be saved forever and can be retrieved at any time should you want it. A quick note: I always recommend using the -m flag when committing, because it keeps you from dealing with writing a commit message in Atom or Vim, which to a new programmer can be a huge headache.

git commit -m "Detailed commit message"    // Bread and butter.
git commit -am "Detailed commit message"   // The "a" allows you to add/commit at the same time!


There’s a basic intro to Git! Although this is plenty for you to start with, there’s a lot more to it. You may want to look up the following topics for more Git tips and tricks:

  • adding a remote and pushing to Github/Bitbucket
  • remote collaboration
  • merging and dealing with merge conflicts
  • branching

Happy coding!