如何让git只在当前目录下工作?

时间:2014-11-27 18:48:45

标签: git bash github

我为我的点文件创建了一个git存储库,但是当我在我的主目录中使用git init时,似乎它也将所有子目录都视为git的一部分。 我已经通过git add remote添加了存储库。 问题是我的bash显示我实际上在哪个分支,例如,用户在分支机构的文件夹中的用户。因此,当我进入我的桌面(〜/桌面)时,它仍然被认为是一个git目录,我甚至可以在那里使用git状态。但是,我想git只考虑存储库的主目录。

2 个答案:

答案 0 :(得分:1)

(编辑:下面的第三个选项,GIT_CEILING_DIRECTORIES,可能是最好的)

除非您明确告诉git在哪里找到当前存储库的gitdir,否则它必须找到它的唯一方法是向上搜索。确实没有其他选择:你告诉它,或者向上搜索。

最近我可以通过插入无效的gitdir引用来破解那些目录中的git

echo gitdir: this isn\'t part of any containing repository >$notinthisone/.git

并告诉包含的存储库忽略它:

echo $notinthisone >> .gitignore

以下

$ cd $notinthisone
$ git status
fatal: Not a git repository: this isn't part of any containing repository
来自包含存储库的

git add将跳过它 - 如果您尝试强制它,您将收到上述错误消息。


有关实现dotfile repo的替代方法,您可以配置存储库以使用您想要的任何工作树。所以:

mv .git somewhere/out/of/the/way
cd !$
git config core.worktree $OLDPWD

echo '*'     >> .gitignore
echo '!.*'   >> .gitignore

git add .gitignore
git commit -m 'ignoring un-hidden files'

生活和学习,在阅读git文档检查.git目录中所有可能的隐藏覆盖时,我遇到了这个:

GIT_CEILING_DIRECTORIES

This should be a colon-separated list of absolute paths. If set, it is a list of directories that git should not chdir up into while looking for a repository directory. It will not exclude the current working directory or a GIT_DIR set on the command line or in the environment. (Useful for excluding slow-loading network directories.)

因此,导出并在.bashrc等效内容中执行相同操作,例如

export GIT_CEILING_DIRECTORIES=$HOME

并且git将向上递送到您的主目录。然后在您的主目录

echo '*'   >>.gitignore
echo '!.*' >>.gitignore 

并且git永远不会向上递送到您的主目录' s repo

答案 1 :(得分:0)

我想我找到了这个帖子的解决方案:Git ignore sub folders @mgold的回答。他建议添加

*/*

到gitignore。如果你使用基于linux的系统,你可能需要改变正则表达式,它在Windows中对我有用。我希望这会有所帮助。

相关问题