For years I used Eclipse, and one of the things I liked most about it was its version control feature. Occasionally (okay, often) I would start to modify a working script/program, and after half an hour would realise that I had introduced a bug, which would be really difficult to find. Eclipse had a great solution: you could simply right-click on the file, and restore to it an earlier timestamp. And, it did all of this version control automatically, without any input from me.
Git is a version control system developed by the guy who made Linux. That was enough for me; you can read more about it in the usual place. Now, not only is Git great at version control, but it also has other features, such as allowing a team of developers to work on a large project simultaneously. But, in this post we're just going to look at how to use it for version control. There is a great tutorial here which describes more features, but the point of this post is to allow someone who has never used/installed Git before to get up and running in just a few steps.
Step 1: Install Git on your computer
Installing on Linux is easy; if you are using a Debian-based system, like Ubuntu, in a terminal type:
$ signifies the terminal prompt)
sudo apt-get install git
On a Mac, it should already be installed, and you can check by typing:
in a terminal. If it is not already installed, you'll be asked if you want to install it.
One way to install it is with
brew install git
brew on OSX, see this post: Installing Homebrew on OSX.
More Git settings
To add a few more Git options, for example, using Vim as the editor for commit messages, or more options for displaying the commits, see this post: More Git Fu.
Step 2: Get an online repository account
Git has a great feature which allows you to backup your version-controlled scripts to an online repository (kind of like Dropbox). This step is not strictly necessary if you just want to keep you backups on your local machine, but we're going to assume you want to do this.
Choose either Github or Bitbucket. These are two companies which essentially offer free online storage for your code. At the end of this post, I have a short discussion comparing the two options, but let's assume you're using Bitbucket.
Sign up for a free Bitbucket account. Choose your username carefully; you're going to be typing it a lot. Also, as obvious as this sounds, remember your password/write it down somewhere...
Step 3: Create an online repository
A 'repository' (online or local) is a place where files are stored.
Sign in to your Bitbucket account. On the toolbar at the top, click on 'Create' and then 'Create repository'. Give it a name, e.g.
myrepo, and fill in a brief description.
You should then be redirected to page named Repository setup. Underneath the subheading Command line, click on 'I'm starting from scratch'. The fourth line should be something like:
git remote add origin https://firstname.lastname@example.org/username/myrepo.git
Copy/paste this somewhere for later; this line will link the local repository we're going to create to the online repository. If for some reason you didn't get redirected to the page described above, the line above can be adapted to your needs by replacing 'username' with your Bitbucket username.
Step 4: Create a local repository
Finally, with the preliminaries out of the way, we can start using Git. Create a folder
myrepo on your computer and move to that folder:
Later, we're going to sync this local
myrepo to the online
myrepo in your Bitbucket/Github account.
First, we'll create some files/folders in this local
myrepo (for example):
echo 'happy days' > file1.txt
echo 'more happy days' > ./folder2/file2.txt
Or you could simply copy/paste some files into the local
Step 5: Initialise Git in this local repo
Really easy, in the
myrepo folder, simply type in the terminal:
This will create a hidden
.git folder which will control the Git magic in this folder. Basically, this has made the folder into a local Git repository. At the moment, the repository is empty.
Step 6: Connect to the online repository
Remember that line we saved in Step 3? It is time to use it. In a terminal, type:
git remote add origin https://email@example.com/username/myrepo.git
username twice and
myrepo with the appropriate names)
You may be asked for your Bitbucket password (hopefully it is written down somewhere...)
Step 7: Add the files to the Git repository
Also really easy, simply type:
git add .
This basically tells Git to take notice of any changes you have made in the files/subfolders, and to add any files/subfolders to the Git index. This step can be repeated many times, but it is the next step which tells Git to actually record these changes by taking a snapshot.
Step 8: Take a snapshot (commit)
Okay, we have added some files or made some changes, and want to take a snapshot of the current situation, i.e. a version to which we can return later. In a terminal type:
git commit -m 'this is my first snapshot'
Obviously, you can replace 'this is my first snapshot' with any other message you prefer. There will be some output like:
2 files changed, 2 insertions(+) create mode 100644 file1.txt create mode 100644 folder2/file2.txt
showing what changes have been made (the Git repo was empty before).
Step 9: Push local files to online repository
To upload the local files to the online repository, in a terminal type:
git push -u origin master
There will be some output, something like:
Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (5/5), 330 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://firstname.lastname@example.org/username/myrepo.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
If you go to your online repository account, and click on the Bitbucket icon in the top left (or Dashboard then Overview), you should see a page listing your online repositories, and next to
myrepo, it should say that it was updated a few seconds ago.
Updating your repo
You've done all the hard work by setting up the local repo.
Now, you can add/modify the files in your local
myrepo, and repeat Steps 7 to 9 as often as you like.
Testing out version control
Part of the reason we may use a version control system such as Git is that, at some point, we may want to revert to an earlier version of the project. Perhaps in the working version we have introduced a bug, and feel that it may be easier to simply go back to the last (good/working) version, rather than spend time trying to find and fix the bug.
As you can guess, Git is great at this, and the next post describes a couple of ways to use it to revert to an earlier version of your project.
Deleting local copy of git repo
Suppose you have a local copy of a Git repo called
temprepo that you want to delete. If you just try the usual
rm -r ./temprepo
you will end up being prompted with a ton of 'override blah blah?' questions, which can be annoying.
Fortunately, there are ways to avoid this, as detailed in this post. The fastest command seems to be:
rm -rf .git
Then, you can delete the folder easily, as usual (after first exiting the folder):
rm -r temprepo
Just thought it might be useful, one day.
Github or Bitbucket?
Github offers unlimited public repositories for unlimited collaborators, and you pay per private repository. Bitbucket offers unlimited private repositories, but you pay per collaborator. As you can guess, 'public' means your code is publicly available, while 'private' means that it is not. Whichever you choose is up to you, but as a single user just wanting to keep a private backup of my work, Bitbucket suits my needs better. I have no affiliation with either company, but in this post, I assume Bitbucket is being used.
This was a very brief introduction just to get you up and running. After using Git for a while, you'll probably be wanting to find out what else it can do. Then, it is worth checking out a more detailed (and better written) tutorial, such as this one. There is also a free online book which looks quite good. Finally, the Git help is also quite good: in a terminal, simply type
git help -a
to see the different commands/topics, and a topic can be accessed simply by typing the command name at the end, e.g.
git help commit
You'll then be in Vim, so
k will move up/down, and
q will return you to the terminal.