Create Getting the latest version of MAR (code only) authored by Grailet Jean-François's avatar Grailet Jean-François
---
title: Getting the latest version of MAR (code only)
---
If you're already a MAR user, there's a good chance you already have set up a `~/MAR` folder somewhere on a cluster or laboratory machine (e.g., climato.be for ULiège researchers), where you would find (some of) the content stored in a MAR repository such as [ESPECES/MAR/stable](https://gitlab.uliege.be/especes/mar/stable).
However, for various reasons, there's also a good chance you have already tuned some files present on this repository for your own needs, typically in `~/MAR/bin` and `~/MAR/usr`. Therefore, if you cloned or pulled the entire [ESPECES/MAR/stable repository](https://gitlab.uliege.be/especes/mar/stable) in your ``~/MAR`` folder, you may overwrite some of your own scripts in the process.
In this tutorial, you will learn how you can get only a subset of files and/or folders from a ``git`` repository. This boils down to running a few ``git`` commands to set up a _sparse checkout_ of a repository, a feature introduced in [version 2.25 of ``git``](https://github.blog/open-source/git/highlights-from-git-2-25/) (2020).
## Pre-requisites
First of all, make sure ``git`` is available on the computer where you want to synchronize your ``~/MAR`` folder with a repository from this GitLab group. You do not necessarily need the latest version, but make sure to have at the very least [version 2.25](https://github.blog/open-source/git/highlights-from-git-2-25/).
Second, on the same computer, run this command:
```
git config --global init.defaultBranch main
```
This will ensure ``git`` always name the first branch of a new repository as ``main``. Indeed, ``git`` use the name ``master`` for the first branch upon initializing a repository, but GitLab rather uses ``main``. This could result in ``git`` mistakingly considering ``master`` and ``main`` as distinct branches, rather than the same one, which is especially a problem if you intend to contribute to a GitLab repository. The ``--global`` option in the above command ensures this behaviour is applied to all your ``git`` commands.
Note that you can easily check your global ``git`` parameters by viewing the content of the ``~/.gitconfig`` file (on a Linux system). In this case, ``cat ~/.gitconfig`` should display the following.
```
[init]
defaultBranch = main
```
## Setting up _sparse checkout_ of a ``git`` repository, step by step
For this example, we will assume you want to get the `src/forMAR/forMAR` folder from the [ESPECES/MAR/stable repository](https://gitlab.uliege.be/especes/mar/stable). In other words, you want to get the latest stable version of MAR.
1. For a smooth process, make sure first that there is no existing `src/forMAR/forMAR` folder or symbolic link in your `~/MAR` folder. In the former case, change the name of the folder (e.g., `src/forMAR/forMAR-old`) if you want to keep a copy of it, and in the latter case, delete the symbolic link.
2. Still in your `~/MAR` folder, initialize ``git``.
```
git init
```
3. Add the [ESPECES/MAR/stable repository](https://gitlab.uliege.be/especes/mar/stable) as a remote source named ``origin``, as follows.
```
git remote add origin https://gitlab.uliege.be/especes/mar/stable.git
```
4. Enable _sparse checkout_.
```
git config core.sparseCheckout true
```
5. Add the path you want to get from the target repository, in this case ``src/forMAR/forMAR/*`` from [ESPECES/MAR/stable](https://gitlab.uliege.be/especes/mar/stable), to the `git` configuration stored in `~/MAR/.git`.
```
echo "src/forMAR/forMAR/*" >> .git/info/sparse-checkout
```
6. Get the latest version of the source code of MAR with a pull.
```
git pull origin main
```
## Updating your repository again
If you did all previous steps correctly, the next time you want to update, you will only need to run the final command from the previous section (while in `~/MAR`).
```
git pull origin main
```
## Adding other paths to the _sparse checkout_
If you want to expand your _sparse checkout_ to get other parts of a repository, you can edit `.git/info/sparse-checkout` (e.g., with ``vim`` or ``gedit``) in the folder where you initialized ``git`` (`~/MAR`, in this case) and add more paths or patterns. The `.git/info/sparse-checkout` file itself is interpreted by ``git`` more or less in the same way as a ``.gitignore`` file.
For example, if you want to also extract the latest version of NESTOR from [ESPECES/MAR/stable](https://gitlab.uliege.be/especes/mar/stable), you can edit `.git/info/sparse-checkout` in `~/MAR` to have the following content.
```
src/forMAR/forMAR/*
src/NESTOR/NESTOR/*
```
After saving changes, tell ``git`` to update itself with respects to the new _sparse checkout_ configuration with the following command (again, to run in `~/MAR`).
```
git sparse-checkout reapply
```
\ No newline at end of file