Friday 8 February 2013

Using git for personal use

Using git with just a local repository
My colleague Martin Hunt gave a nice presentation yesterday on using git to keep track of different versions of your scripts. I've decided to use it straight away, so in my scripts directory I typed:
% git init
This made a subdirectory .git.
You never have to typte 'git init' again from now on. 

If I then type:
% git status
it tells me that all the files currently in my scripts directory are untracked files.

I can add the latest perl script that I wrote by typing:
% git add my.pl
If I type 'git status' again, it tells me that there has been one file added ('my.pl').

I can commit the file by typing:
% git commit -m "Nice perl script to do some things"
If I type 'git status' again, then 'my.pl' is no longer listed as a file that has just been added.

If I edit my.perl, I can commit the changed version by typing:
% git add my.pl
% git commit -m "Added nice subroutine"
This will commit the changed version of my.pl.

Using git with a local and a remote repository
My colleagues and I share a remote repository at Sanger for a project that we work on together. In this case, I have a local repository in my directory for that project (note to self: it is /nfs/users/nfs_a/alc/Documents/git/helminth_scripts/), and I then keep copies of them in the shared remote repository too.

Anyone at Sanger can make a copy of the remote repository by 'cloning it':
% mkdir git
% cd git
% git clone git+ssh://git.internal.sanger.ac.uk/repos/git/pathogens/helminth_scripts
This will create a 'helminth_scripts' folder, with all the subdirectories.
From then on, you don't need to type 'git clone' anymore; the local repository has been created.

I can add my latest perl script to the local repository by typing:
% git add my.pl
% git commit -m "Nice perl script to do some things"

When I think the script is working and ready for others to use (this refers to Sanger users only), I can then copy it to the remote repository. Before I copy it to the remote repository, I need to first pull down all new versions of scripts from the remote repository, by typing:
% git pull origin master
I can then copy my script my.pl to the remote repository:
% git push origin master
To make it possible for others at Sanger to use, I need to follow the steps in 'Deploying changes at Sanger' below.

Deploying changes at Sanger
This is mostly a 'note to self' to remind me how to do this. 
When I am happy that a Perl script my.pl that I have written is ready for other users to use, I can 'deploy' it at Sanger, which means that it is put in a special remote git repository for 'stable releases' of scripts, and then this latest stable version is put in the path of other Sanger users. 

To do this:
% ssh lenny-dev64
% newgrp pathdev
% cd /software/pathogen/projects/helminth_scripts
% git pull origin master # pulls the latest changes from git.internal.sanger.ac.uk
% chmod -R g+w .         # some files won't be changed as they belong to other users
% ln -s /software/pathogen/projects/helminth_scripts/scripts/my.pl /software/pathogen/internal/prod/bin/my.pl               # make /software/pathogen/internal/prod/bin/my.pl point to /software/pathogen/projects/helminth_scripts/scripts/my.pl
% ln -s  /software/pathogen/projects/helminth_scripts/modules/HelminthGenomeAnalysis/Mymodule.pm /software/pathogen/internal/prod/lib/Mymodule.pm
% exit
% exit
Note: I need to type 'exit' twice to exit lenny-dev64, because 'newgrp' opens a subshell.

For Farm3, things have changed a little:
% ssh farm3-login
% cd /software/pathogen/projects/helminth_scripts
% git pull origin master # pulls the latest changes from git.internal.sanger.ac.uk
% chmod -R g+w .         # some files won't be changed as they belong to other users
% ln -s /software/pathogen/projects/helminth_scripts/scripts/my.pl /software/pathogen/internal/prod/bin/my.pl               # make /software/pathogen/internal/prod/bin/my.pl point to /software/pathogen/projects/helminth_scripts/scripts/my.pl
% ln -s  /software/pathogen/projects/helminth_scripts/modules/HelminthGenomeAnalysis/Mymodule.pm /software/pathogen/internal/prod/lib/Mymodule.pm
% exit
% exit

Other useful git commands

To stop git from tracking a script, without deleting it from the directory:
% git rm --cached script.pl

To make git exclude some files when you are running 'git status' (eg. .swp files), you can add them to .git/info/exclude (in your main git directory). See this stackoverflow question

See the last version of a file that you committed to git, eg. for file
plot_tree_with_domains_with_ETE.py:
% git show HEAD~1:plot_tree_with_domains_with_ETE.py


Thanks to Martin Hunt & Daria Gordon for explaining git.

No comments: