The example below is simplified to show the core of the problem, not the problem itself(my file tree is more complicated than that).Let's say that I have two files I want to back up; one in ~/somedir/otherdir, the other in ~/otherdir/somedir/. I want to backup files from both directories in one git repository. How can I do this? Soft links only carry information about where file is stored, not the actual file, whereas hard links are somewhat foreign to me. Is this a case where hard links should be used?Clarification: I want to use git because of four reasons: I want to store dotfiles/scripts/configurations that are text files and keep track of changes over time, I know git, I have a private git repository I could use to store them, and I want to be able to share these files across multiple PCs.
How to backup files in multiple directories with git?
backup;git
If you don't mind moving the files...You could do this by moving the files into a git repository, and symlinking them to their old locations; you'd end up with~/gitrepo/somedir/otherdir/file1 moved from ~/somedir/otherdir/file1~/gitrepo/otherdir/somedir/file2 moved from ~/otherdir/somedir/file2a symlink from ~/somedir/otherdir/file1 to ~/gitrepo/somedir/otherdir/file1a symlink from ~/otherdir/somedir/file2 to ~/gitrepo/otherdir/somedir/file2You can then safely commit the files in the git repository, and manipulate them using git, and anything using the old file paths will see whatever is current in the git workspace. Linking the files the other way round, or using hard links, would be dangerous because any git operation which re-writes the file (changing branches, reverting to a previous version...) would break the link. (Hopefully this explains why hard links aren't really a viable solution.)With this kind of scenario you'll have to be careful with programs which re-write files completely, breaking links; many text editors do this, as do tools such as sed -i etc. A safer approach would be to move the entire folders into the git repository, and symlink the directories.If you want to keep the files in place...Another possibility is to create a git repository in your home directory, tell git to ignore everything, and then forcefully add the files you do want to track:cdgit initecho '*' > .gitignoregit add -f .gitignoregit commit -m Initialise repository and ignore everythinggit add -f somedir/otherdir/file1git commit -m Add file1git add -f otherdir/somedir/file2git commit -m Add file2Once you've done this, you'll easily be able to track changes to files you've explicitly added, but git won't consider new files. With this setup it should also be safe to have other git repositories in subdirectories of your home directory, but I haven't checked in detail...