git:如何向存储库添加外部目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2383754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git: How do you add an external directory to the repository?
提问by csexton
I want to add an external directory to an existing repository.
我想将外部目录添加到现有存储库。
External Dir: /home/some/directory
外部目录:/home/some/directory
Working Dir: /htdocs/.git
工作目录:/htdocs/.git
If I attempt the following command from the /htdocs dir:
如果我尝试从 /htdocs 目录执行以下命令:
git add /home/some/directory
git add /home/some/directory
I get an error: fatal: '/home/some/directory' is outside repository
我收到一个错误:致命:'/home/some/directory' 在存储库之外
回答by csexton
If I need to do something like that I would normally move that external file or directory into my git repo and symlink it's original location to the new one.
如果我需要做类似的事情,我通常会将该外部文件或目录移动到我的 git repo 中,并将它的原始位置符号链接到新的位置。
mv /home/some/directory /htdocs/directory
ln -s /htdocs/directory /home/some/
git add ./directory
I use this technique when I am developing a plug-in for an application that I want to keep under version control but have to store in a specific location.
我在为应用程序开发插件时使用此技术,该应用程序希望保持在版本控制下但必须存储在特定位置。
回答by MotherDawg
I had the same error... Googled it to death... not much came out.
我有同样的错误......谷歌搜索死了......没有多少出来。
Christian's answer worked :
克里斯蒂安的回答有效:
git --work-tree=/ add /home/some/directory
But then "work-tree" got me going. I grep'd all Git docs and came up with core.worktree
.
但后来“工作树”让我走了。我搜索了所有 Git 文档并想出了core.worktree
.
I used
我用了
git config --global core.worktree /
And voila! I can now add from anydirectory in the system. I don't know if it will cause problems any other places, but I'll try to update as I go along.
瞧!我现在可以从系统中的任何目录添加。我不知道它是否会在其他任何地方引起问题,但我会在进行时尝试更新。
回答by anon
git --work-tree=/ add /home/some/directory
回答by Stormbytes
There's a really simple solution to this.
对此有一个非常简单的解决方案。
Let say you're Git-repo is in /var/data/my-app and you want to add /var/data/public/ to that repo. You can't use 'add' because Git won't add resources outside of the repo's root directory. So..
假设您的 Git-repo 位于 /var/data/my-app 中,并且您想将 /var/data/public/ 添加到该存储库中。您不能使用 'add',因为 Git 不会在 repo 的根目录之外添加资源。所以..
Solution:
解决方案:
Move the /var/data/public/ directory into your Git-repo root (/var/data/my-app).
Create a sym-link (symbolic link) inside of /var/data/public to the /var/data/my-app/public folder, using:
ln -s source_file_or_directory virtual_file_or_directory
将 /var/data/public/ 目录移动到您的 Git-repo 根目录 (/var/data/my-app)。
在 /var/data/public 内部创建一个符号链接(符号链接)到 /var/data/my-app/public 文件夹,使用:
ln -s source_file_or_directory virtual_file_or_directory
Then, just add the moved file or directory to Git using git add
in the normal fashion. Your Git source control is now tracking the desired file/folder, and your sym-link in the external directory assures that the file/folder is accessible to whatever else!
然后,只需以git add
正常方式将移动的文件或目录添加到 Git中即可。您的 Git 源代码控制现在正在跟踪所需的文件/文件夹,并且您在外部目录中的符号链接确保该文件/文件夹可被其他任何人访问!
回答by Christian Tischer
If the code that you want to add to your repo is not too big, you could also automatically copy it to your repo each time before you push.
如果你想添加到你的 repo 的代码不是太大,你也可以在每次推送之前自动将它复制到你的 repo。
In my case I for example need one R file, which I modify quite often, in several repos and I wrote a little shell script:
例如,在我的情况下,我需要一个 R 文件,我经常修改它,在几个 repos 中,我写了一个小 shell 脚本:
#!/usr/bin/env bash
cp -r /some/directory/file.R .
git add .
git commit -m "add"
git push
...and I run this script in order to push all changes, including the ones in /some/directory.
...我运行此脚本以推送所有更改,包括 /some/directory 中的更改。
回答by Tubbles
This got me thinking, since I would like very much the solution of using a symlinked file/dir but am stuck on a windows computer. As far as I know the symlinks in windows doesnt really work in the same way. So another solution could be to write two scripts/bash functions to "import" and "export" the file(s) in question, such as:
这让我开始思考,因为我非常想要使用符号链接文件/目录的解决方案,但我被困在 Windows 计算机上。据我所知,windows 中的符号链接并没有真正以同样的方式工作。因此,另一种解决方案可能是编写两个脚本/bash 函数来“导入”和“导出”有问题的文件,例如:
import() {
cp -fr /home/some/directory /htdocs/
}
export() {
cp -fr /htdocs/directory /home/some/
}
Then you would have a copy of the file in your repository, which you could git add
.
然后,您将在存储库中拥有该文件的副本,您可以git add
.
回答by superarts.org
Or you can use submodule if you want to work across different git repos (ln doesn't work in this way). Try 'man git-submodule'.
或者,如果您想跨不同的 git 存储库工作,则可以使用子模块(ln 不能以这种方式工作)。试试'man git-submodule'。
回答by Alan Haggai Alavi
Add a symbolic link to the directory within the repository. Then, add the same.
将符号链接添加到存储库中的目录。然后,添加相同的。
ln -s /home/some/directory/
git add directory