1

I am looking in how can I merge existing submodule to my main repository. Basically currently my repository looks somehing like this

├── example1.java
├── example2.java
├── img
|   └── image.jpg
└── submodule
    ├── sub_example1.java
    ├── sub_example2.java
    └── img
        └── sub_image.jpg

And submodule repository itself looks like this

├── sub_example1.java
├── sub_example2.java
└── img
    └── sub_image.jpg

What I am trying to achieve is add this submodule folder inside may main project.

.gitattributes on both repositories looks the same

*.jpg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.odt filter=lfs diff=lfs merge=lfs -text
*.xls filter=lfs diff=lfs merge=lfs -text
*.xlsx filter=lfs diff=lfs merge=lfs -text
*.xla filter=lfs diff=lfs merge=lfs -text

so images from img folders are stored as LFS objects

I have tried to follow some tutorials, for example this one https://stackoverflow.com/a/72557462/8189102 where I would clone main repo and submodule repo beside each other like so

├── main/
└── submodule/

then I would checkout -b a local branch where I want to make changes

$ git checkout -b submodule-merge
$ git submodule deinit -f -- submodule
Cleared directory 'submodule'
error: could not lock config file .git/modules/submodule/config: No such file or directory
warning: Could not unset core.worktree setting in submodule 'submodule'

$ rm -rf .git/modules/submodule
$ git rm -f submodule
rm 'submodule'

Then on the submodule repository I would

$ cd submodule
$ git filter-repo --to-subdirectory-filter submodule
Parsed 501 commits
New history written in 2.12 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
Updating files: 100% (39508/39508), done.
HEAD is now at fa01cf43 Merge branch 'JRA-2221' into 'master'
Enumerating objects: 44721, done.
Counting objects: 100% (44721/44721), done.
Delta compression using up to 4 threads
Compressing objects: 100% (27931/27931), done.
Writing objects: 100% (44721/44721), done.
Total 44721 (delta 13961), reused 44721 (delta 13961), pack-reused 0
Completely finished after 7.69 seconds.

$ ls -l
submodule

$ git lfs ls-files
119926153d - submodule/img/sub_image.jpg

And then when trying to do the final merge

$ cd main
$ git remote add submodule ../submodule/
$ git fetch submodule
remote: Enumerating objects: 44702, done.
remote: Counting objects: 100% (44702/44702), done.
remote: Compressing objects: 100% (27928/27928), done.
remote: Total 44702 (delta 13950), reused 44700 (delta 13949), pack-reused 0
Receiving objects: 100% (44702/44702), 19.83 MiB | 14.37 MiB/s, done.
Resolving deltas: 100% (13950/13950), done.
From ../submodule
 * [new branch]            JRA-117 -> submodule/JRA-117
 * [new branch]            JRA-221 -> submodule/JRA-221
 * [new branch]            JRA-333 -> submodule/JRA-333
 * [new tag]               b-211      -> b-211
 * [new tag]               b-333        -> b-333

$ git merge --allow-unrelated-histories --no-commit submodule/master
error: Your local changes to the following files would be overwritten by merge:
  .gitmodules submodule

$ git status
On branch submodule-merge
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   .gitmodules
    deleted:    submodule

$ git commit -m "Rremoved submodule"
[submodule-merge 1683ee7b89] Rremoved submodule
 Committer: user
 2 files changed, 4 deletions(-)
 delete mode 160000 submodule

$ git merge --allow-unrelated-histories --no-commit submodule/master
Automatic merge went well; stopped before committing as requested

$ git status also shows now all the new files added under submodule folder. Then I commit all the changes

$ git commit -m "Added submodule"
[submodule-merge 332f599182] Added submodule
 Committer: user

$ git status
On branch submodule-merge
nothing to commit, working tree clean

Next I try to push the changes to new branch and I get the following error regarding LFS object

$ git push origin submodule-merge
Locking support detected on remote "origin". Consider enabling it with:
  $ git config lfs.https://gitlab.example.com/main/main.git/info/lfs.locksverify true
Unable to find source for object 65cbf95fd1c7984cc8aca10ef81901e59e5bd42e05d177d1f7b6c95237a20b49 (try running git lfs fetch --all)
Uploading LFS objects:   0% (0/96), 0 B | 0 B/s, done.
error: failed to push some refs to 'gitlab.example.com:main/main.git'

And I get stuck here with no idea what to do. Also when running $ git lfs ls-files | grep 65cbf95f on both repositories (main and submodule) There are no results, its like this object has reference somewhere but actually it does not exist.

I would be very thankful if someone could help me to solve this. Thank you!

4
  • See my comment to a similar question. I believe the only way to do what you want is to remove LFS from the submodule, merge and then migrate the merged repository to LFS. Commented May 23, 2023 at 16:40
  • @phd Ok I tried to run $ git lfs migrate export --include="*" --everything first on submodule repo and also git filter-repo --path .gitattributes --invert-paths --force to remove .gitattributes file, and then adding this remote to main repo, and after running git merge I get Encountered 1 file(s) that should have been pointers, but weren't: submodule/img/sub_image.jpg But I was able to push it to my branch, but now this image file does not show up as LFS pointer. How can I now migrate it to LFS? I would like not to git lfs fetch --all, because the actual repo is like 25GB. Commented May 24, 2023 at 10:44
  • Could anything from stackoverflow.com/q/46704572/7976758 help? Commented May 24, 2023 at 11:09
  • 1
    Looks like running git add --renormalize . after git merge --allow-unrelated-histories --no-commit submodule/master did actually help and I was able to push to main repository which uploaded submodule/img/sub_image.jpg as LFS object. Thank you @phd Commented May 25, 2023 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.