Initialization of Git LFS and Submodules

Initialize LFS and Submodules

To initialize a Git repository within a folder, including a subfolder’s Git repository and large files (using Git Large File Storage, LFS), you need to initialize LFS and submodules:

  1. Install Git LFS: Visit the Git LFS website to learn how to install it.

  2. Initialize the top-level folder as a Git repository: Open the command line or terminal, switch to the top-level folder path, and run the following command to initialize a new Git repository:

    1
    
    git init
  3. Add Git LFS support: In the same directory, run the following command to enable Git LFS:

    1
    
    git lfs install
  4. Configure LFS to track large files: Determine the large files or file types to be tracked by LFS. Use the git lfs track command to specify. For example, if you want to track all .zip files, you can run:

    1
    
    git lfs track "*.zip"

    After executing this command, it will add the relevant configuration to the .gitattributes file. Make sure to add the .gitattributes file to the repository:

    1
    
    git add .gitattributes
  5. Add the subfolder’s Git repository as a submodule: If the subfolder is already a Git repository and you want to maintain its independent update and commit history, you can add it as a submodule. Use the following command, where path_to_subfolder is the relative path of the subfolder:

    1
    
    git submodule add <repository_url> path_to_subfolder

    Note: You need to replace <repository_url> with the remote repository URL of the subfolder’s Git repository. If the subfolder’s Git repository does not have a remote repository, you need to create a remote repository for the subfolder first.

  6. Commit and Push: Once LFS tracking is configured and all necessary files are added, you can add these changes to the Git repository and commit them:

    1
    2
    
    git add .
    git commit -m "Initial commit with LFS and submodule"

    If the top-level repository has a corresponding remote repository, use the git push command to push the changes to the remote repository.

By following the above steps, you can successfully initialize a Git repository in a top-level folder containing other large files and subfolder Git repositories and enable Git LFS.

LFS Isolation

When using Git and Git Large File Storage (LFS), the LFS configuration is typically for the entire repository, not for individual remote repositories. This means that when LFS is enabled and certain file types are tracked, those LFS-tracked files will be processed when pushing to any remote repository.

However, if you want to avoid pushing LFS-tracked files to a specific remote repository, you need to manage LFS objects and non-LFS objects using two separate branches, and then only push the branch that does not contain LFS objects to the remote repository where you don’t want LFS objects.

1. Maintain Two Branches

  • LFS Branch: This branch contains files tracked by LFS. You can work on this branch as usual.
  • Non-LFS Branch: This branch does not contain any files tracked by LFS. You can maintain this state by using .gitattributes filtering rules or simply ensuring that large files are not committed to this branch.

2. Branch Strategy

When preparing to push changes to a remote repository, first determine whether LFS objects need to be included.

  • If LFS objects are needed, push the changes to the corresponding remote repository branch as usual.
  • If LFS objects are not needed, merge the changes (excluding LFS files) into the non-LFS branch, and then only push this branch to the specific remote repository.

3. Example Operations

Assume main is the primary working branch (LFS branch), and non-lfs-branch is the non-LFS branch. When pushing to a remote repository where LFS files are not desired, you can operate as follows:

  1. Ensure non-lfs-branch is up to date and includes all non-LFS changes from the main branch.

    1
    2
    
    git checkout non-lfs-branch
    git merge main --no-commit --no-ff

    During the merge process, if there are changes that include LFS files, you need to manually resolve these differences to ensure LFS files are not included.

  2. After completing the merge, push non-lfs-branch to the remote repository where LFS files are not needed.

    1
    
    git push <remote-name> non-lfs-branch:<remote-branch-name>

    Replace <remote-name> and <remote-branch-name> with the target remote repository name and branch name.

Buy me a coffee~
Tim AlipayAlipay
Tim PayPalPayPal
Tim WeChat PayWeChat Pay
0%