How to Check If a Git Repository Uses LFS

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a Git repository is utilizing Git Large File Storage (LFS). We will explore the key indicators of LFS usage, starting with examining the .gitattributes file, which is crucial for configuring LFS tracking.

You will then use the git lfs env command to verify the LFS environment and settings within the repository. Finally, we will test with LFS-tracked files to confirm that LFS is actively managing large files as expected. This hands-on approach will provide you with the practical skills to identify and understand LFS integration in your Git projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/SetupandConfigGroup(["Setup and Config"]) git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git/SetupandConfigGroup -.-> git/config("Set Configurations") git/SetupandConfigGroup -.-> git/git("Show Version") git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/commit("Create Commit") subgraph Lab Skills git/config -.-> lab-560102{{"How to Check If a Git Repository Uses LFS"}} git/git -.-> lab-560102{{"How to Check If a Git Repository Uses LFS"}} git/add -.-> lab-560102{{"How to Check If a Git Repository Uses LFS"}} git/status -.-> lab-560102{{"How to Check If a Git Repository Uses LFS"}} git/commit -.-> lab-560102{{"How to Check If a Git Repository Uses LFS"}} end

Check for .gitattributes File

In this step, we will explore how Git LFS uses a special file called .gitattributes to track large files. This file tells Git which files should be handled by LFS instead of being stored directly in the Git repository.

First, let's make sure we are in our project directory. Open your terminal and type:

cd ~/project/my-time-machine

Now, let's create a dummy file that we will later tell Git LFS to track. We'll call it large_image.png.

echo "This is a large image file placeholder." > large_image.png

This command creates a file named large_image.png and puts some text inside. In a real scenario, this would be a large binary file like an image, video, or archive.

Next, we need to tell Git LFS to track this type of file. We do this using the git lfs track command. This command also automatically creates or updates the .gitattributes file in your repository.

git lfs track "*.png"

You should see output similar to this:

Tracking "*.png"

This command tells Git LFS to track all files with the .png extension. It also adds an entry to the .gitattributes file.

Now, let's look at the contents of the .gitattributes file to see what was added.

cat .gitattributes

You should see output similar to this:

*.png filter=lfs diff=lfs merge=lfs -text

This line in the .gitattributes file is the magic that tells Git to use LFS for .png files. It specifies that for files matching the pattern *.png, Git should apply the lfs filter for diffing, merging, and storing. The -text part ensures that Git doesn't try to treat binary files as text.

Understanding the .gitattributes file is key to using Git LFS effectively. It's how you configure which types of large files are managed by LFS, keeping your main Git repository small and fast.

Run git lfs env to Verify LFS

In this step, we will use the git lfs env command to check the Git LFS environment and confirm that it is set up correctly within our repository. This command provides useful information about the Git LFS installation and configuration.

Make sure you are still in your project directory:

cd ~/project/my-time-machine

Now, run the git lfs env command:

git lfs env

You should see output similar to this (the exact output may vary slightly depending on the Git LFS version and system configuration):

git-lfs/3.x.x (Linux; zsh)
git version 2.34.1

Endpoint=
  https://github.com/labex/my-time-machine.git/info/lfs (auth=none)
Endpoint (SSH)=
  [email protected]:labex/my-time-machine.git/info/lfs (auth=none)

LocalWorkingDir=/home/labex/project/my-time-machine
LocalGitDir=/home/labex/project/my-time-machine/.git
LocalGitStorageDir=/home/labex/project/my-time-machine/.git/lfs
LocalMediaDir=/home/labex/project/my-time-machine/.git/lfs/objects
LocalRecentObjectsDir=/home/labex/project/my-time-machine/.git/lfs/objects/sha256/recent
LocalRecentObjectsLogDir=/home/labex/project/my-time-machine/.git/lfs/objects/sha256/recent/logs
LocalTmpDir=/home/labex/project/my-time-machine/.git/lfs/tmp
LocalTriggers=

Disk space utilization:
  du -s -h .git/lfs
  4.0K    .git/lfs

Git LFS initialized.

This output provides a lot of information, including:

  • The version of Git LFS being used.
  • The version of Git being used.
  • The LFS endpoints (where LFS files would be pushed/pulled from, though we won't be using a remote in this lab).
  • The local directories where Git LFS stores its data within the .git directory.
  • Disk space utilization by LFS objects (currently very small as we haven't added any large files yet).
  • Confirmation that Git LFS is initialized in this repository.

Running git lfs env is a good way to quickly verify that Git LFS is installed and configured correctly in your current repository. It helps you understand where LFS is storing its data and confirms that the necessary hooks and filters are in place.

Test with LFS-Tracked Files

In this final step, we will add the LFS-tracked file to the Git staging area and commit it. This will demonstrate how Git LFS handles the file differently from regular files.

First, ensure you are in the correct directory:

cd ~/project/my-time-machine

We have already created the large_image.png file and configured Git LFS to track .png files in the previous steps. Now, let's add the file to the staging area using git add:

git add large_image.png

This command stages the file for the next commit. Git LFS intercepts this operation for large_image.png because it matches the pattern in .gitattributes. Instead of adding the entire file content to the Git index, Git LFS adds a small "pointer" file.

Let's check the status to see the staged file:

git status

You should see output indicating that large_image.png is a new file ready to be committed:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitattributes
        new file:   large_image.png

Notice that .gitattributes is also staged because we modified it in Step 1.

Now, let's commit the changes:

git commit -m "Add LFS tracked image placeholder"

You should see output similar to this:

[master (root-commit) abcdefg] Add LFS tracked image placeholder
 2 files changed, X insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 large_image.png

The commit message confirms that both .gitattributes and large_image.png were committed. However, the actual content of large_image.png is not stored directly in the Git commit. Instead, the commit stores the small pointer file, and the large file content is managed by Git LFS.

To see the difference, let's look at the content of the file as stored in Git using git show. This command shows the content of a file at a specific commit.

git show HEAD:large_image.png

You should see output similar to this:

version https://git-lfs.github.com/spec/v1
oid sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
size 31

This is the content of the pointer file, not the actual "This is a large image file placeholder." text. This demonstrates that Git is storing a reference to the large file, while Git LFS is managing the actual file content.

This is the core concept of Git LFS: keeping the main Git repository lightweight by storing only pointers to large files, while the large file content is stored separately.

Summary

In this lab, we learned how to check if a Git repository uses Git LFS. We started by understanding the role of the .gitattributes file, which is crucial for configuring Git LFS. We created a dummy file and used git lfs track to tell Git LFS to manage files with a specific extension, observing how this command automatically updates the .gitattributes file with the necessary configuration. We then examined the contents of the .gitattributes file to see the entry added by git lfs track, understanding the meaning of the filter=lfs, diff=lfs, merge=lfs, and -text attributes.

We also explored the git lfs env command as another method to verify the presence and configuration of Git LFS within a repository. This command provides detailed information about the Git LFS environment, including the version, repository settings, and tracked file patterns, offering a comprehensive overview of how LFS is set up. Finally, we learned how to test the LFS tracking by adding and committing a file that matches the configured pattern, confirming that Git LFS correctly handles the large file instead of storing its full content in the main Git repository.