Git:如何忽略/指定 *checkout* 的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5494576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:15:22  来源:igfitidea点击:

Git: How to ignore/specify files for *checkout*

git

提问by iain

If I don't want .html files tracked I can add the pattern to .gitignore and they'll be ignored. I'd like to know how I can do the converse - at checkout, how could I ask git to only checkout certain types of files or not checkout certain types of files?

如果我不想跟踪 .html 文件,我可以将模式添加到 .gitignore 并且它们将被忽略。我想知道如何进行相反的操作 - 在结帐时,我怎么能要求 git 只结帐某些类型的文件或不结帐某些类型的文件?

For example, if I didn't want html files I could write:

例如,如果我不想要 html 文件,我可以这样写:

git checkout HEAD . --no .html

if that existed :) Is there a way already built in, or do I just have to run something else post-checkout?

如果存在:) 有没有内置的方法,或者我只需要在结账后运行其他东西?

Any help is much appreciated.

任何帮助深表感谢。

回答by Cascabel

If you want to package up files for deployment, you probably don't need - or want - the repo itself. This is exactly what git archiveis for. A couple examples from the manpage (linked):

如果您想打包文件以进行部署,您可能不需要 - 或者不需要 - 存储库本身。这正是git archive它的目的。联机帮助页中的几个示例(链接):

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the /var/tmp/junk directory.

git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip > git-1.4.0.tar.gz

Create a compressed tarball for v1.4.0 release.

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

创建一个包含当前分支上最新提交内容的 tar 存档,并将其解压缩到 /var/tmp/junk 目录中。

git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip > git-1.4.0.tar.gz

为 v1.4.0 版本创建一个压缩的 tarball。

You ought to be able to get it to do exactly what you want, with the help of the export-ignoreattribute:

export-ignore属性的帮助下,您应该能够让它完全按照您的意愿行事:

export-ignore

Files and directories with the attribute export-ignore won't be added to archive files. See gitattributes(5)for details.

export-ignore

具有 export-ignore 属性的文件和目录不会添加到存档文件中。有关详细信息,请参阅gitattributes(5)

For example, to exclude the directory privateand the files mine.txtand secret.c, you could put in the file .gitattributes:

例如,要排除目录private和文件mine.txtand secret.c,您可以放入文件.gitattributes

private/     export-ignore
secret.c     export-ignore

Just like gitignore files, you can put those anywhere in your repository, and they'll operate from that directory, but starting from the top level is a good bet.

就像 gitignore 文件一样,您可以将它们放在存储库中的任何位置,它们将在该目录中运行,但从顶级开始是一个不错的选择。

回答by Simon East

Git's Sparse-Checkout

Git 的稀疏结帐

If you only want to checkout a portion of your repository, you can use git's sparse-checkout option which has powerful include/exclude rules.

如果您只想签出存储库的一部分,则可以使用 git 的 sparse-checkout 选项,该选项具有强大的包含/排除规则。

The following StackOverflow question has some helpful instructions:
GIT checkout except one folder

以下 StackOverflow 问题有一些有用的说明:
GIT checkout except one folder

But as a summary:

但作为总结:

  1. Enable the sparseCheckoutoption:

    git config core.sparseCheckout true
    
  2. Create the file called .git/info/sparse-checkoutcontaining:

    /*
    !node_modules
    

    which effectively means include everything except the node_modulesdirectory when checking out a repository into the working directory.

  1. 启用该sparseCheckout选项:

    git config core.sparseCheckout true
    
  2. 创建名为.git/info/sparse-checkout包含的文件:

    /*
    !node_modules
    

    这实际上意味着node_modules在将存储库签出到工作目录中时包括除目录之外的所有内容。

回答by Kristian

So I wanted to ship my code without the test files to the production server.

所以我想将没有测试文件的代码发送到生产服务器。

Easiest way for me was just to remove all the test files with: rsync --exclude 'tests'after unpacking the archive.

对我来说最简单的方法就是rsync --exclude 'tests'在解压存档后删除所有测试文件:

回答by Cameron Skinner

You cannot retrieve part of an individual commit in Git. You either pull a commit or you don't - there's no half-way step.

您无法在 Git 中检索单个提交的一部分。您要么提交,要么不提交 - 没有半途而废。

This means that if there is a commit that adds a bunch of files then you can get all of those files (by pulling that commit into your local repo) or none of them (by not pulling that commit).

这意味着,如果有一个提交添加了一堆文件,那么您可以获取所有这些文件(通过将该提交拉入您的本地存储库)或不获取所有文件(通过不拉该提交)。

You canchoose which commits you want with git cherry-pick, but I suspect it's not going to help you much in this case.

可以选择你想要的提交git cherry-pick,但我怀疑在这种情况下它不会对你有太大帮助。

Generally a file should either be in version control or not - I can't think of a reason why you'd want a file in Git only sometimes. If there's something in your .gitignore then it's probably a good candidate for removing from the repository altogether. .gitignore basically means "don't look at this file, it's not under version control".

通常一个文件要么在版本控制中,要么不在版本控制中——我想不出为什么你只是有时想要一个文件在 Git 中。如果您的 .gitignore 中有某些内容,那么它可能是完全从存储库中删除的好选择。.gitignore 基本上意味着“不要看这个文件,它不受版本控制”。

(If you don't mind rewriting your repository history you could cherry-pick the add commit, then do a git reset --mixed HEAD^1and create new commits that leave out the files you don't want...but don't do this unless you really know what you're doing. Better to just remove the files and push a new commit that records the removal.)

(如果您不介意重写您的存储库历史记录,您可以选择添加提交,然后执行git reset --mixed HEAD^1并创建新提交,将您不想要的文件排除在外……但除非您真的知道,否则不要这样做你在做什么。最好只删除文件并推送记录删除的新提交。)

EDIT: Just saw your comment about wanting specific parts for a deployment. In that case I'd suggest you create a deployment branch that removes the irrelevant files. You can rebase this branch from mainline whenever you need to and it should magically remove the files for you each time.

编辑:刚刚看到您关于需要特定部件进行部署的评论。在这种情况下,我建议您创建一个部署分支来删除不相关的文件。您可以在需要时从主线重新设置此分支,并且每次都应该神奇地为您删除文件。