我可以将.git文件夹存储在我想跟踪的文件之外吗? [英] Can I store the .git folder outside the files I want tracked?
问题描述
我有一个不寻常的想法,使用git作为备份系统。假设我有一个目录./backup/myfiles,我想用git来支持它。为了保持干净,我不想在myfiles文件夹中有.git目录,所以我想我可以创建./backup/git_repos/myfiles。从看git文档,我试过这样做:
$ $ $ $备份/ myfiles
$ mkdir。 ./git_repos/myfiles
$ git --git-dir = .. / git_repos / myfiles init
在backup / git_repos / myfiles /
$ git --git-dir中初始化的空的Git仓库=../ git_repos / myfiles /add foo
fatal:pathspec'foo'不匹配任何文件
您可以看到我到达的错误消息。我在做什么错了?
git --git-dir = .. / repo --work-tree =。添加foo
这会做你想做的,但是当你必须指定每个git您可以输出 GIT_WORK_TREE =。
和 GIT_DIR = ..您可以输出 $ b / backup
,Git会在每个命令中选择它们。尽管如此,这只能让你在每个shell的单个仓库中工作。
我宁愿建议将.git目录符号链接到其他位置,或者创建符号链接到您的主备份目录中的.git目录。
I have an unusual idea to use git as a backup system. So let's say I have a directory ./backup/myfiles and I want to back that up using git. To keep things clean I don't want to have a .git directory in the myfiles folder, so I thought I could create ./backup/git_repos/myfiles. From looking at the git docs, I've tried doing this:
$ cd backup/myfiles
$ mkdir ../git_repos/myfiles
$ git --git-dir=../git_repos/myfiles init
Initialized empty Git repository in backup/git_repos/myfiles/
$ git --git-dir="../git_repos/myfiles/" add foo
fatal: pathspec 'foo' did not match any files
You can see the error message I get there. What am I doing wrong?
git --git-dir=../repo --work-tree=. add foo
This will do what you want but will obviously suck when you have to specify it with every git command you ever use.
You can export GIT_WORK_TREE=.
and GIT_DIR=../backup
and Git will pick them up on each command. That will only comfortably allow you to work in a single repository per shell, though.
I’d rather suggest symlinking the .git directory to somewhere else, or creating a symlink to the .git directory from your main backup directory.
这篇关于我可以将.git文件夹存储在我想跟踪的文件之外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!