我正在尝试将一些代码移动到新的git repo,但我不想从现有的repo中复制整个文件。如果我可以为其他仓库创建一个符号链接并引用这个新仓库中的代码,那么我认为会很好。
答案 0 :(得分:2)
如果您尝试避免多个存储库中的重复文件,可以使用submodule feature来实现此目的。
一个人为的例子:
# 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
答案 1 :(得分:0)
为什么不[副]要复制文件?它们非常大吗? - Schwern
@Schwern,你做得对 - Arvin
虽然@chuckx is right that you can use submodules as repository symlinks,我不相信这个问题实际上是关于符号链接的。这是OP选择的解决方案。这是一个XY Problem。真正的问题是如何处理非常大的文件,并避免让它们搞砸你的存储库。因为通常Git会存储每个版本的完整版本,以及可能变得非常非常大的媒体文件(二进制,大型和已经压缩)。
有一种更好的方法可以解决这个问题:Git Large File Storage。这将大型文件存储在云存储中,同时仍然像存储在Git中的普通文件一样工作。最受欢迎的Git服务器支持git-lfs
。对于大型文件存储而言,这是一个更优雅和跨性别的解决方案,而不是将它们放在自己的存储库中并使用子模块。