A small tutorial for Git Annex, the Git LFS alternative

A small tutorial for Git Annex, the Git LFS alternative
Photo by Leiada Krozjhen / Unsplash

Git Annex is a powerful tool that allows you to manage large files in your Git repositories without storing them directly in the Git history. With Git Annex, you are more free than with Git LFS as you have the entire control of where your files are stored. For example, with Git LFS, it is mandatory to store your files in the same service that you use to store your code (e.g. Github LFS if you store your files on Github), but with Git Annex, you can choose where to store it without any limitation.

Initializing Git Annex

Before you can start using Git Annex, you need to download it for your OS using the official website: https://git-annex.branchable.com/install/

Cloning the Project

To clone your project, use the following command:

git clone [PROJECT]

Initializing Git Annex

Once you have cloned the repository, you need to initialize Git Annex. You can give your device a name (this is optional but recommended for better organization):

git annex init "DEVICE_NAME"

Configuring .gitattributes

To manage the limits between files stored in Git and those in Git Annex, add a .gitattributes file with the following content:

annex.largefiles=(largerthan=50MB) # Limit file size for Git

Here, the files that are larger than 50MB will be sent automatically to git annex instead of git by using git annex add .

Setting Up a Remote

Next, git annex have to know where to store our large files. For this example, we will use a directory:

git annex initremote REMOTE_NAME type=directory directory=PATH_TO_DIRECTORY encryption=none

By doing this, all the large files will be sent to a repository after a sync.

Adding Files

To add files to Git Annex, use the following command:

git annex add .

This command will track all files in the current directory. If a file is smaller than the limit you put in your .gitattributes file, it will be added automatically to Git

Synchronizing Files

To synchronize the files added to Git Annex with their respective remotes, use:

git annex sync --content

The --content option ensures that the files are sent to their respective remotes.

You can also add the -m parameter to add a commit message.

Setting Up for a Second User

If you have another user who needs access to the repository, they should repeat the cloning and initialization steps. They will also need to enable the remote:

git annex enableremote source directory=PATH_TO_DIRECTORY

Additional Information

  • All user computers are considered remotes in Git Annex.
  • The command git annex sync --content performs a git annex pull followed by a git annex push. If there are conflicts, Git Annex resolves them automatically by sending both versions of the file, with the filename differing slightly.