Using gitHub and gitLab

Git is version control software i.e. it can be used as a way to back up your work, collaborate with others and to go back to earlier versions if you need to. The files are kept in repositories ("repos" to the cool kids!) - the local repo is in your local file storage and the web-based repo (the 'remote') is hosted on a server, for example or The remote repos can be open to the public or kept private.

You may have used GitHub in the past as somewhere to put your code/data for journal publications but you may not have used it when developing your work. This page gives some links to more info on git and a brief overview of how to use it once you have git on your machine.

You may use GitHub or GitLab but GitLab is simpler to deal with in terms of logging in as you do not need to acquire a personal access.


Training material

For a presentation on gitlab basics by Nick Schurch - see pptx:

git basics pptx

GUI/Command line options

You can use the git GUI in windows or tortoise tools - see:

If you want to use the command line, you need to use linux commands. If you need a refresher on some basic commands, see Linux Commands

Git in Rstudio

For a handy guide by Katherine Whyte see Git in Rstudio

How to use gitLab - command line summary

To summarise, you need to set up a repository on gitlab, then clone it to a local repo (use the https option under the clone button), then you can add files, commit them and push back to gitlab, e.g.

git clone <gitlab url>

echo 'hello World!' > newfile.txt

git add newfile.txt

git commit -m 'added newfile.txt'

git push

How to use github - command line summary

I will give a brief overview below but see for more details.

Firstly make sure you have git on your machine (try typing 'git') and then declare a folder/directory on your machine as a git repo (this is done locally - no connection with the internet required!). To do this on linux, go into the folder you want and type:

git init

To save files to your local repo you need to tell git to add the files and then you do a commit (you must add a comment describing this commit). So basically it is a two stage process to save your files to the repo each time, eg. to add the Rscripts in your folder:

git add *.R

git commit -m 'add all my Rscripts'

You can view what git believes has changed at any point with:

git status

If you want to then copy these files to the web based repo on github, you go to and make a new repository.

Then in your local repository create a remote branch with the short name 'origin' (for example).

Finally, 'push' your files to it.

git remote add origin

git push origin master

Note, you will need a personal access token (PAT) to push to the web repo - see

From now on, you can work as usual and when you want to commit to the local repo you simply add the appropriate files, commit them, then push to the remote repo as required:

git add *.R

git commit -m 'some new things happened' ##do not use this comment!

git push origin master

Top tip (for any git work)

Generally you shouldn't track extra output files that are generated automatically on each computer (e.g. R project files, R history). Within the main folder of your repo, consider including a text file called .gitignore.txt which includes a list of file types (or specific files/folders) you wish not to track or upload to your repo. Lots of example templates are available (; see R.gitignore for the R example), and check that you include *.Rproj. Note: this file, and any changes to it, should be committed to your repo.