Git in 30 Minutes!

I was telling a friend about the benefits of using version control, Git in particular, when writing papers. He was of course convinced (I’m very persuasive) and he said “Well, now you have to teach me Git”. I told him that I could tell him all I knew in 30 minutes. Not that Git is simple, it’s actually pretty big and there are whole books written about it. But most of the complicated stuff are useful to me so scarcely that I forget them no matter how many times I review them.

So here goes my 30-minute Git tutorial to get you started. You can learn the more complicated tools and techniques here, for instance.

Getting Started

Unlike SVN, Git is not centralized, meaning that everyone has a local copy of the repository and you don’t need a remote server to use Git with a project. You can either start with a local repository (and perhaps put in on a server later on), or clone an existing repository from a server.

Create A Local Repository

This is very simple. Navigate to the root directory of your project and run

$ git init

Clone A Remote Repository

You need to run

$ git clone <repository_url>

The exact form of <repository_url> and what happens next depend on your server set up. When using HTTPS, you will be asked for a password. Setting up SSH authentication is a bit more complicated. If you are using Bitbucket, the documentation is detailed and thorough. The same goes for GitHub.

Adding Files

Now that you have a repository, you need to add files. The following command will add the files main.java and ui.java and the directory io to the index (stage them for the next commit):

$ git add main.java ui.java io

Committing Changes

To commit the files already staged for commit (in the index):

$ git commit

This will open an editor, so that you can enter a message for the new commit. Make sure to write a reasonably descriptive message that says what has changed in this commit, and never leave the message blank. You will thank me later.

Working with a Remote Repository

To see all remotes and their details for your repository:

$ git remote -v

If you have cloned your repository from a server, you will have at least the source server listed under the name origin. To add a remote:

$ git remote add <name> <url>

where <name> is the name you will use in future for this remote.

Once you have at least one remote, you can simply use git push and git pull to interact with it. Both commands can optionally be followed by name of a remote.

Basic Branching

Branching is a way to diverge from the main line of development, while protecting the main line from any unnecessary (or even harmful) changes. Say you want to test a new feature that requires making significant changes to your code. You can simply create a branch, implement and test your changes, and merge it back with the main code if and when you are happy with it. By default you deal with the branch master.

To list all branches:

$ git branch

To create a new branch called <branch>:

$ git branch <branch>

To checkout (switch to) an existing branch:

$ git checkout <existing-branch>

To merge a branch into the current branch:

$ git merge <branch>

Finally, to delete a branch once you are done with it:

$ git branch -d <branch>

The above command works only if you have merged the changes. If you want to delete a branch even though it has unmerged changes (i.e. you don’t want to keep the changes), run

$ git branch -D <branch>

Of course, Git branching is a lot more powerful than this and in fact is one of the things that sets Git apart from other VCSs. If you want to get any deeper, Git’s branching tutorial is a good place to start.

Other Common Tasks

Finally, here are some tasks that do not fit into the above categories, but are quite useful nevertheless. I will add to this list in the future.

To add to the index and commit all modified and deleted files (but not any new files):

$ git commit -a -m "commit message"

Note that I have put the message in the command as well. This is of course not good if there are too many changes and multi-line message is more appropriate.

To discard working tree changes (i.e. all changes made since last commit – be careful with this command!):

$ git checkout .