How to git a folder outside the repository?
Asked Answered
T

3

6

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?

Threesome answered 15/11, 2014 at 23:55 Comment(1)
This question about letting git follow symlinks has some nice answers/optionsAzarcon
D
5

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.
Dosage answered 16/11, 2014 at 0:16 Comment(3)
agreed, the 3rd option: sudo mount --bind SOURCEDIRECTORY TARGETDIRECTORY does work indeedExigent
How to undo this mount?Exigent
sudo umount SOURCEDIRECTORY and sudo umount TARGETDIRECTORY should work.Dosage
A
0

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.

Au answered 16/11, 2014 at 0:7 Comment(0)
P
0

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
Phrenic answered 16/11, 2014 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.