Getting Started with Git and Bitbucket

When you are writing software code and/or scripts, it is helpful to keep a history all your changes. Then if you make a mistake or aren't happy with the direction your code is heading, you can go back to a previous version. One efficient way to do this is via a version-control system.

Using simple commands, you can track all changes that you make. These changes will be stored in a hidden repository on your computer. But it is also very useful to store your repository in a remote location like BitBucket. This serves at least two purposes: 1) backup and 2) sharing. If your computer crashes, you will easily be able to get your code back if it is stored online. But it also enables you to share your work more easily with me and with other scientists. One nice thing about BitBucket is that you can restrict access to your online repository while you are actively working on it. You can indicate specific users who can see it. Then when you are ready to share it with the world, you can easily do so. Usually I keep my repositories private until I submit a journal article that describes the project.

For simple needs (updating and retrieving code), Git is easy to use and can be automated. But it also provides extensive advanced options for those who want to use them. Here is a simple overview of the basic commands you would use (please excuse the author's French!). But you can also find many other tutorials on the Web as well as books to help you along.

Let's say you were starting a new project. First you would create a new repository on Bitbucket. After creating it, you will see instructions to set up the repository on your local computer at the command line. Click where it says, "I'm starting from scratch" and follow their example to initialize the repository on your computer.

Next create a file called ".gitignore" that lists which files you will want to exclude from your repository. Most likely you will exclude any data files or binaries (such as figures or executables). List these, even if you have not already created them. For example:

Figures
Data
*.txt

After writing code, you would use the following command to indicate that you want to add any new files that have been created. It also would identify any files that had been added previously and then deleted.

git add --all .

The next step is to "commit" the changes using a command such as the following:

git commit -m "Message describing the changes".

Lastly, you would push the local repository to BitBucket:

git push origin master

Please make sure that you commit and push all code changes at the end of each research session (or even more frequently than that). And be sure to give Dr. Piccolo (user ID: srp33) access to your repository so he can access your code.