Git

How to Add a Single File to GIT LFS?

LFS stands for Large File Storage. Git LFS replaces large files with the text pointer in Git while keeping the file’s content on remote servers, such as GitHub. While working on large development projects, developers must create and manage multiple files. However, Git is not optimized for managing large files because adding large files to the Git repository can affect the whole project. In this situation, Git LFS is designed to manage large files efficiently. More specifically, it helps users to share large files and collaborate with them.

This write-up will illustrate the procedure of adding a single file to the Git LFS.

How to Add a File to Git LFS?

To add a single file to the Git LFS, follow the given-provided steps:

    • Navigate to the Local Directory.
    • List Repository Content.
    • Choose a particular file.
    • Add file to Git LFS using the β€œgit lfs track –filename <file-name>” command.
    • Stage and commit changes to the Git repository.
    • Verify changes.

Note: Git LFS is already installed in the latest version of Git. However, you can install its extension manually using the provided link.

Step 1: Redirect to Local Repository

First, type out the below-listed command and navigate to the local repository:

cd "C:\Git\Repo1"

 
Step 2: View Repository Content

Next, list the current repository’s content:

ls

 
It can be seen that the repository contains two text files. Now, choose the desired file that needs to be added to Git LFS. For instance, we have selected that β€œnew.txt” file:


Step 3: Add File to Git LFS

Then, utilize the provided command along with the desired file name for adding it to the Git LFS:

git lfs track --filename new.txt

 

Step 4: Track Changes

After that, add changes to the Git index:

git add .

 

Step 5: List LFS Files

Now, view the LFS content to ensure that the desired file has been added to it:

git lfs ls-files

 
The below output indicates that the β€œnew.txt” file has been added to the LFS successfully:


Step 6: View LFS Status

Run the following command to check the current status of the LFS:

git lfs status

 
It can be observed that the LFS changes need to be committed:


Step 7: Commit Changes

Next, commit LFS changes to the Git repository:

git commit -m "Adding Changes"

 

Step 8: Verify Status

Lastly, verify that the file changes have been staged and committed:

git lfs status

 
As you can see that the Git status is clear now which indicates there is nothing to be staged or committed:


That was all about adding a single file to the Git LFS.

Conclusion

To add a single file to the Git LFS, first, select the desired file that needs to be added to the Git LFS. Then, execute the β€œgit lfs track –filename <file-name>” command to add it to the Git LFS. After that, stage and commit file changes. This write-up illustrated the method of adding a single file to the Git LFS.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.