我可以将 .git 文件夹存储在我想要跟踪的文件之外吗? [英] Can I store the .git folder outside the files I want tracked?

查看:36
本文介绍了我可以将 .git 文件夹存储在我想要跟踪的文件之外吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的想法,即使用 git 作为备份系统.所以假设我有一个目录 ./backup/myfiles 并且我想使用 git 备份它.为了保持干净,我不想在 myfiles 文件夹中有一个 .git 目录,所以我想我可以创建 ./backup/git_repos/myfiles.通过查看 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

这会做你想做的事,但是当你必须用你曾经使用的每个 git 命令指定它时显然会很糟糕.

This will do what you want but will obviously suck when you have to specify it with every git command you ever use.

您可以导出 GIT_WORK_TREE=.GIT_DIR=../backup 并且 Git 会在每个命令上选取它们.不过,这只会让您轻松地在每个 shell 中的单个存储库中工作.

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.

我宁愿建议将 .git 目录符号链接到其他地方,或者从主备份目录创建指向 .git 目录的符号链接.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆