...and then it crashed

Programming the web with Python, Django and Javascript.

Git makes writing code easier!

Git is a distributed version control system that’s a quite tricky to get into, and it can be hard to justify spending time getting to know it.

By the end of this post, I hope you’ll be in a better position to use Git on all your software projects, and understand the benefits of doing so.

Git is easy to start using.

Installing Git on your system is simply a case of selecting the correct Git installer, and downloading the software onto your computer. Once you’ve got Git installed, setting up a Git repositity for your software project is as simple as typing the following commands into a terminal:

1
2
$ cd /path/to/your/project
$ git init

Commit your code to Git as often as possible

The more frequently you commit, the easier it is to go back at a later date and understand the work you’ve been doing (and maybe even undo some of that work). Committing is easy!

1
2
$ git add .
$ git commit -a -m "Description of commit"

If you’ve made a terrible mistake, simply roll back to the last commit

This is why it’s a good idea to commit your work frequently. If you make a stupid coding mistake, and want to revert your code back to how it was before you broke everything, then just run the following command:

1
$ git revert --hard

Go back time and see how your code used to look

Just type the following command to display a history of all your past commits:

1
$ git log

To revert your codebase to some time in the past, simply copy the corresponding commit hash to your clipboard, close the Git log by pressing q, then type the following command:

1
$ git checkout YOUR_COMMIT_HASH -- .

(A commit hash looks a bit like this: 814c219a338006492bf6f751d958461dd3e8b775)

Once you’ve finished with the older version of your code, you can go back to the lastest version by running the following command:

1
$ git reset --hard

Alternatively, if you want to keep this older version of your code (and discard any changes you’ve made since then), simply commit it using the following commands, and keep working:

1
2
$ git add .
$ git commit -a -m "Rolled back to previous version of code"

Back up your code on a server

To protect against hard drive failure, it’s a good idea to back up your code. You can either set up your own code hosting service, or save yourself some effort and get a free BitBucket account.

Once you’ve created a remote repository, you can connect your local codebase to it using the following commands:

1
2
$ git remote add origin ssh://bitbucket.org/your-repo-name.git
$ git push origin master -u

Then, whenever you’ve made a few commits that you want to push to the server, just run the following command:

1
$ git push

Work with other people

Once you’ve put your code online, you can invite other people to work on it too. In order to get a copy of your code, they just need to run the following command:

1
2
$ git clone ssh://bitbucket.org/your-repo-name.git
$ cd your-repo-name

They can then make changes, commit them, and push them to the server. In order for you to see the changes that they have make, just run the following command on your machine:

1
$ git pull

Sometimes things go wrong when you push

If you try to push your code, and you get an error message saying this:

1
`! [rejected] master -> master (non-fast-forward)`

Don’t worry, just run git pull, and try pushing again.

Sometimes things go wrong when you pull

If you try to pull some code, and get an error message saying this:

1
CONFLICT (content): Merge conflict in some_file

Don’t worry, this just means that two people have tried to edit the same file. Just open the conflicting file in your editor, fix the contents, and run the following commands:

1
2
$ git add .
$ git commit -a -m "Merging in changes"

Can’t I just use Dropbox?

Dropbox is pretty good. However, for coding projects, Git has some key advantages:

  1. With Git, you choose when to save a version, and those versions get meaningful descriptions.
  2. Git is extremely good at merging changes made by multiple people to the same file.
  3. Git can save versions, and rollback to previous versions, even when offline.
  4. When you understand Git, and know some of it’s more complex features, it can do magic.

Comments