How to git a folder outside the repository?

913 views Asked by At

I have a git repository sitting as ~/a.

At the same time, I have some stuff from ~/b/content/data/, which will be updated by another application.

For backup purposes, I would like to add ~/b/content/data/'s stuff into git ~/a, without moving the folder. And also, of course, without manual copy.

Can I do that? Is it via ln?

3

There are 3 answers

0
jthill On

More ways to do it

git --work-tree=~/b/content/data add .

will do it for one-time use (the pathname you specify is relative to the given worktree)

Check whether the worktree you specify has any nested repositories in it (find that/path -name .git), those are also known as submodules and what will be added to your own repository is just the currently-checked-out commit id there.

Note that checkout like add is always to the current worktree, so if you do the above command, then commit, then checkout without the override, you'll get ~/b/content/data's innards checked out under ~/a.

If you want to set a persistent worktree for the repository,

git config core.worktree ~/b/content/data

or as a relative path (relative to the .git directory)

git config core.worktree ../../b/content/data  # ~/a/.git/.. is ~/a
0
TimePath On

If you link ~/b/content/data to ~/a/b, git will only store a reference to the pathname, not the actual contents. On the other hand, if you move ~/b/content/data to ~/a/b and link it back to ~/b/content/data, git will commit the files.

3
user2291758 On

Adding symlinked directories worked until git 1.6.1. Now you have other options.

  • You can make hardlinks for the individual files you want to store, if you know them or they are only few.
  • You can put the actual data into the repository and make ~/b/content/data/ a symlink.
  • You can use sudo mount --bind SOURCEDIRECTORY TARGETDIRECTORY instead of a link.