Reputation: 594
I'm trying to move a some code to a new git repo, but i don't want to copy the entire files from the existing repo. So was thinking would be nice if I can create a symlink for the other repo and reference the code in this new repo
Upvotes: 1
Views: 515
Reputation: 164679
Why do you don't [sic] want to copy the files? Are they very large? - Schwern
@Schwern, you got it right - Arvin
While @chuckx is right that you can use submodules as repository symlinks, I don't believe the question is really about symlinks. That's the OP's chosen solution. It's an XY Problem. The real problem is what to do with very large files and avoid having them gum up your repository. Because normally Git will store a complete version of every revision, and for media files (binary, large, and already compressed) that can get very, very large.
There's a much better way to solve this problem: Git Large File Storage. This stores large files in cloud storage while still working like normal files stored in Git. Most popular Git servers support git-lfs
. It's a much more elegant and transpartent solution to the large file storage than putting them in their own repo and using submodules.
Upvotes: 0
Reputation: 6877
If you're trying to avoid duplicate files in multiple repositories, you can use the submodule feature for this purpose.
A contrived example:
# create directories for two repositories
$ mkdir repo1
$ mkdir repo2
# initialize repo1
$ cd repo1
$ git init
Initialized empty Git repository in /home/chuckx/code/stackoverflow/git-submodule-example/repo1/.git/
touch remote.txt
# add a file to repo1
echo remote file content > remote.txt
$ git add remote.txt
$ git commit -m "Add remote.txt"
[master (root-commit) b98953d] Add remote.txt
1 file changed, 1 insertion(+)
create mode 100644 remote.txt
# initialize repo2
$ cd ..
$ cd repo2
$ git init
Initialized empty Git repository in /home/chuckx/code/stackoverflow/git-submodule-example/repo2/.git/
# add a file to repo2
$ echo local file content > local.txt
$ git add local.txt
$ git commit -m "Add local.txt"
[master (root-commit) 2b9d7ff] Add local.txt
1 file changed, 1 insertion(+)
create mode 100644 local.txt
# add repo1 as a submodule of repo2
$ git submodule add ../repo1
Cloning into '/home/chuckx/code/stackoverflow/git-submodule-example/repo2/repo1'...
done.
$ ls
local.txt repo1
# create a symlink within repo2 to a file in repo1
$ ln -s repo1/remote.txt
$ ls
local.txt remote.txt repo1
$ cat remote.txt
remote file content
# commit submodule and symlink additions to repo2
$ git add remote.txt
$ git commit -m "Adding rep1 submodule and symlink to repo1/remote.txt"
[master 88a505a] Adding rep1 submodule and symlink to repo1/remote.txt
3 files changed, 5 insertions(+)
create mode 100644 .gitmodules
create mode 120000 remote.txt
create mode 160000 repo1
# update the file in repo1
$ cd ../repo1/
$ echo update >> remote.txt
$ git add remote.txt
$ git commit -m "Update remote.txt"
[master fc53bd3] Update remote.txt
1 file changed, 1 insertion(+)
# fetch the changes from repo1 to the submodule instance in repo2
$ cd ../repo2/
$ git submodule update --remote repo1
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/chuckx/code/stackoverflow/git-submodule-example/repo1
b98953d..fc53bd3 master -> origin/master
Submodule path 'repo1': checked out 'fc53bd3372ba288e2604bf9ccfa408011c9de228'
# show the updated file content from repo1 within repo2
$ cat remote.txt
remote file content
update
Upvotes: 2