Git LFS (Large File Storage) is an extension for Git that allows you to manage large files efficiently in Ubuntu 22.04 by replacing large files in your repository with small pointer files, while storing the actual file contents on a remote server.
Here’s how to install and initialize Git LFS on Ubuntu 22.04:
sudo apt-get install git-lfs
git lfs install
What is Git LFS?
Git LFS, or Git Large File Storage, is an extension for Git that allows users to manage large files more effectively. It replaces large files in your repository with lightweight pointer files, while storing the actual content on a remote server. The primary purpose of Git LFS is to streamline the handling of files that are too large or too numerous to be efficiently tracked by Git in its standard form.
Using Git LFS provides several advantages:
- It resolves the performance issues that occur when working with large binary files or datasets.
- It simplifies collaboration by reducing repository size and speeding up cloning and fetching operations.
- It enables versioning of large media files, making it easier to track changes over time.

Prerequisites
System Requirements
To start using Git LFS on Ubuntu 22.04, you should ensure that your system meets a few basic requirements. You will need:
-
A properly installed version of Git. You can confirm this by running:
git --version
-
Basic system packages for support. Ensuring you have `curl` installed is recommended:
sudo apt update sudo apt install curl

Installing Git LFS on Ubuntu 22.04
Step 1: Downloading the Git LFS Package
Begin by downloading the Git LFS installation script. This step will set up the repositories necessary for installation:
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
Step 2: Installing the Package
Once the repository is ready, you can install Git LFS using the following command:
sudo apt install git-lfs
To ensure that Git LFS has been installed correctly, you can check the version with:
git lfs --version
If you see the version information displayed, your installation was successful!

Configuring Git LFS
Initializing Git LFS
After installing Git LFS, you need to initialize it in your repository. This allows you to start using its features.
git lfs install
Tracking Large Files
To take advantage of Git LFS, you must specify which files to track. This can be done using the `git lfs track` command. For example, to track Photoshop files, you would use:
git lfs track "*.psd"
You can easily track multiple file types by executing:
git lfs track "*.mp4" "*.zip"
Modifying .gitattributes File
When you track files with Git LFS, a `.gitattributes` file is created (or modified) in your repository. This file lists all the large file types you've chosen to track. You can view the contents by running:
cat .gitattributes
Make sure it includes entries for each file type you're tracking.

Using Git LFS in Your Workflow
Adding and Committing Large Files
When you add large files to your Git LFS-tracked repository, you can still use Git’s normal `add` and `commit` commands:
git add <large_file>
git commit -m "Add large file example"
What happens here is that Git LFS stores the actual file separately and replaces it with a pointer file. This keeps your repository lightweight.
Pushing Changes to Remote Repository
After committing changes, you’ll need to push your files and commits to a remote repository. The command remains the same:
git push origin main
Git LFS ensures that the pointer files are pushed, while the actual large file content is uploaded to the LFS storage.

Retrieving Large Files from a Remote Repository
Cloning a Repository with LFS Files
When you clone a repository that contains LFS files, you can do so in the familiar way:
git clone <repository-url>
This will clone the repository and download the pointer files. However, at this stage, the actual large files are not retrieved yet.
Fetching LFS Files After Initial Clone
To pull the actual large files into your local copy, execute:
git lfs pull
This command fetches the LFS-managed content that corresponds to the pointer files in your local repository.

Managing LFS Files
Listing Tracked Files
If you ever need to check which files are tracked by Git LFS, you can do so with:
git lfs ls-files
This is a handy command to verify your large file tracking.
Untracking Files
If you find that you no longer need to track a specific file type with Git LFS, you can untrack it:
git lfs untrack "*.psd"
Make sure to edit your `.gitattributes` accordingly to remove the entry for the untracked file.
Cleaning Up LFS Storage
Over time, your LFS storage may accumulate unreferenced files. To clean it up, use the following command:
git lfs prune
This command helps you manage storage efficiently by removing any large files that are no longer referenced in your repository.

Troubleshooting Common Issues
Dealing with Missing Files
If you encounter messages about missing LFS files during checkouts or pulls, make sure you have correctly set up the LFS tracking and that you’ve pulled the files after cloning.
Understanding Git LFS Logs
For any issues that arise, Git LFS logs can provide valuable insight. You can view logs by accessing:
cat .git/lfs/logs
These logs can help diagnose what has gone wrong during file transfers or tracking setups.

Conclusion
Integrating Git LFS into your workflow on Ubuntu 22.04 allows you to handle large files efficiently, improving both performance and collaboration. By understanding how to install, configure, and utilize Git LFS effectively, you can manage large files seamlessly alongside your code. We invite you to share your experiences with Git LFS and encourage you to delve deeper into its many capabilities.

Additional Resources
For more in-depth understanding, consider looking at the official [Git LFS documentation](https://git-lfs.github.com/) and exploring tutorials that cover advanced Git techniques. Participating in community forums can also be beneficial for troubleshooting and learning from others’ experiences.