git: How do you add an external directory to the repository?
GitAddGit Problem Overview
I want to add an external directory to an existing repository.
External Dir: /home/some/directory
Working Dir: /htdocs/.git
If I attempt the following command from the /htdocs dir: > git add /home/some/directory
I get an error: fatal: '/home/some/directory' is outside repository
Git Solutions
Solution 1 - Git
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.
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.
Solution 2 - Git
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
.
I used
git config --global core.worktree /
And voila! I can now add from any directory 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.
Solution 3 - Git
git --work-tree=/ add /home/some/directory
Solution 4 - Git
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..
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
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!
Solution 5 - Git
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.
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:
#!/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.
Solution 6 - Git
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:
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
.
Solution 7 - Git
I would make hard links to each of the files in the external directories. As hard link does not make additional overhead over the original file, it could be a perfect sync. The only problem is that a directory cannot be linked (only by super-user), so it must be created to store files.
For example, normally zeppelin support version controls only for notebook files, but I added hard links of the conf files in the parent directory as follows:
.
├── 2A94M5J1Z
│ └── note.json
├── 2G2U2ZR1T
│ └── note.json
├── conf
│ ├── shiro.ini
│ ├── zeppelin-env.sh
│ └── zeppelin-site.xml
└── README.md
Solution 8 - Git
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'.
Solution 9 - Git
use this code to move the directory if you use windows git bash terminal and make a symlink to source
git mv <source_directory> <destination_directory>
git ln -s <source_directory>
Solution 10 - Git
Add a symbolic link to the directory within the repository. Then, add the same.
ln -s /home/some/directory/
git add directory