GIT结帐除了一个文件夹 [英] GIT checkout except one folder

查看:100
本文介绍了GIT结帐除了一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结账到其他分支,或之前的提交,但我想保留一个文件夹相同的文件(不签出文件夹)。



我希望该文件夹将显示在 git status 中,因此我现在可以将此文件夹添加到索引。



例如,我有一个包含 node_modules 的文件夹。我希望文件夹在我的所有提交中(我知道大多数人喜欢.gitignore node_modules 文件夹)。但是当我移动到其他提交或检查其他分支时,我希望 git 不会触及 node_modules 文件夹。



是否有可能?

解决方案

您可以使用稀疏结帐来排除 node_modules 目录从工作树。正如文档所述:


稀疏结帐允许稀疏地填充工作目录。它使用 skip-worktree 位告诉Git工作目录中的文件是否值得一看。


以下是您如何使用它的方法。首先,启用 sparseCheckout 选项:

  git config core.sparseCheckout true 

然后,添加 node_modules 路径作为否定 .git / info / sparse-checkout 文件中:

  echo -e/ * \\\
!node_modules>> .git / info / sparse-checkout

这将创建一个名为 sparse的文件-checkout 包含:

  / * 
!node_modules

这实际上意味着忽略 node_modules 目录时,将提交的树读入工作目录

如果您稍后想要包含 node_modules 目录(即删除 skip-worktree code>位),您必须修改稀疏检出文件以仅包含 / * - 即包含所有路径 - 并使用 git read-tree 更新您的工作目录:

  echo/ *> .git / info / sparse-checkout 
git read-tree -mu HEAD

您可以然后通过将其配置变量设置为 false 完全禁用稀疏检出:

  git config core.sparseCheckout false 

请注意稀疏结帐 Git 1.7.0


I want to checkout to other branch, or previous commit, but I want to keep one folder the same files (not checkout the folder).

I want that the folder, will be shown in git status, so I can add this folder now to the index.

For example, I have a folder with node_modules. I want the folder to be in all my commits (I know that most of the people prefer to .gitignore the node_modules folder). But when I move to other commit, or checkut other branch, I want that git will not touch the node_modules folder.

Is it possible?

解决方案

You could use sparse checkout to exclude the committed contents of the node_modules directory from the working tree. As the documentation says:

"Sparse checkout" allows populating the working directory sparsely. It uses the skip-worktree bit to tell Git whether a file in the working directory is worth looking at.

Here's how you use it. First, you enable the sparseCheckout option:

git config core.sparseCheckout true

Then, you add the node_modules path as a negation in the .git/info/sparse-checkout file:

echo -e "/*\n!node_modules" >> .git/info/sparse-checkout

This will create a file called sparse-checkout containing:

/*
!node_modules

which effectively means ignore the node_modules directory when reading a commit's tree into the working directory.

If you later want to include the node_modules directory again (i.e. remove the skip-worktree bit from its files) you have to modify the sparse-checkout file to only contain /* – that is "include all paths" – and update your working directory using git read-tree:

echo "/*" > .git/info/sparse-checkout
git read-tree -mu HEAD

You can then disable sparse checkout altogether by setting its configuration variable to false:

git config core.sparseCheckout false

Note that sparse checkout was first introduced in Git 1.7.0.

这篇关于GIT结帐除了一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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