Friday, March 29, 2013

Advice for new programmers: Use git

Advice for new programmers: Use git

Version control is awesome. It's purpose is to allow programmers to collaborate without stepping on each other toes. However the benefits of a tool like git extend into situations where there is only a single programmer.

Why version control?

  • Stop losing your code Without version control, There are so many ways to lose your code: deleting the wrong file, closing a window when you left the changes in the undo stack, uploading the wrong file version through FTP. Sometimes this isn't a big deal, but other times it can cost hours or days of work.
  • Sync your code across work stations. If you have a remote repository it's trivial to sync your code across multiple.
  • Freedom to experiment A seasoned engineer once told me that git can be used like a "crash helmet". This is the most unexpected benefit. If you think you're going to try something wacky, branch and it it works, use it, if it sucks, leave it.
Using git has several advantages that pay off immediately. Mercurial is similar, but I know git, so here we go.


Debian (Ubuntu/Mint/Whatever)

    sudo apt-get install git


If your os doesn't come with git, you can download binaries from the website

Getting started

To start a local git repository
    git init
Create a readme and add it to the repo
    git add
or to add all the new files.
    git add --all
Then to commit your changes
    git commit -m "What you did"
To see that you in fact, did commit.
    git log

Branching and merging

When experimenting, or making a new feature, its usually a good idea to create a new branch. Branching and merging with git is really easy.
To make a new branch
    git checkout -b newbranch
To see that you in fact, did switch branches
    git branch
So after you'd made some changes and want to merge back into master.
    git checkout master
    git merge newbranch
If their are no conflicts the commits will go straight in and you now have everything on master. When done with a branch its a good idea to remove them, otherwise things could get messy.
    git branch -d newbranch

Remote account

To make your code available everywhere you have to have a remote repo. I use Github for my open source projects since the community is larger. If there is any reason you want to keep the code private (probably a good idea for in progress classes), Bitbucket offers unlimited private repos for small teams.
Once you have your new repo set up you can link it to your local repo.
    git remote add origin http://path/to/remote/repo
Origin is the default remote repository, it can be called anything, but origin is the norm.
To push your code to the remote repository:
    git push origin master
Every time a change is made locally, to add to the remote repo. Simply:
    git add --all
    git commit -m "What you did"
    git push
Now your code is stored on the internet!

Working on multiple machines

Once you have a remote repo you can get your code onto a new computer. On your second computer enter.
    git clone http://path/to/remote/repo
Now the code is on the new machine.
After making some changes and pushing to a new repo, you'll want to get your changes back onto the original computer. To do thing you have a couple options.
    git pull
    git fetch && git rebase
Git pull performs a fetch (meaning get the changes), and then merge them into your current branch. Git rebase attempts to perform local and remote changes in order an avoid merging. Both will work. However pulling will add a bunch of merge commit messages into your log, which you might not want. You WILL want to you git pull if you think there might be a lot of conflicts.


Git is super awesome. It is critical in a group context, and very useful in an individual context. I would have had a much easier time in college and early pro career, if I knew about git.

Helpful applications:

bash: grb (command line utility that makes working with remote branches easier)
linux: git-cola
osx: (source tree)

No comments:

Post a Comment