将文件一次包含到存储库然后忽略后续更改的正确方法是什么? [英] What is the proper way to include a file once into a repository and then ignore subsequent changes?

查看:134
本文介绍了将文件一次包含到存储库然后忽略后续更改的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我为第一个版本提交的目标文件和程序集文件。我不想提交对这些文件的更改,因为我对项目进行了更改并更新了其他源文件。但是,我想在第一次提交时将它们保存在我的存储库中。



我知道的两个选项是:


  1. 从存储库中删除它们: 1

    a。更新.gitignore

    b。 git rm --cached< file> 或...


  2. git update-index --assume-unchanged< file> 2


这两个都可以在这个问题中看到:忽略已经提交给Git仓库的文件



和here:



Git:忽略在提交和提交两个特定文件。






1 .. 。但是,我希望他们留在版本库中,并在下一次提交完成后将其删除。



2 .. 。此选项根据此问题的答案注释:



< blockquote>

假设 - 不应该被忽略机制滥用。它是我知道我的文件系统操作很慢......特别是,Git没有承诺Git会一直认为这些路径是未经修改的......它保留报告该路径已被修改的权利(作为结果,git commit -a可以自由地提交该更改)。

那么是否有合适的方法来提交这些文件一次,将它们保持在存储库中的状态,然后忽略对它们的进一步更改?



我在这里找到了一些信息: https://gist.github.com/canton7/1423106



但它似乎...涉及。是否没有任何实际的选项,git包含的工作就好像--assume-不变,但没有撒谎?

解决方案

截至 Git 1.7.0 你有第三个选项:< 稀疏结帐



<对于稀疏结账,你基本上告诉Git从工作树中排除一定数量的文件。这些文件仍然是存储库的一部分,但它们不会显示在您的工作目录中。



在内部,稀疏结帐使用 skip- worktree 标志将所有排除文件标记为始终更新。从文档


在读取条目时,如果它被标记为skip-worktree,那么Git
假装其工作目录版本是最新的,并读取
索引版本。


以下是如何在现有的存储库
$ b $ ol <

  • 启用稀疏结帐 git config core.sparseCheckout true

  • 创建一个 .git / info / sparse- checkout 文件,其中包含工作目录中 include 的路径。在你的情况下,你必须包含所有内容,然后通过在其路径前添加来排除
    特定文件:



    / *
    !文件路径到


  • 使用 git read-tree -mu HEAD

    更新您的工作目录


    请注意,如果您想要取回排除的文件,仅使用禁用稀疏检出功能是不够的> git config core.sparseCheckout false 。您首先必须修改 .git / info / sparse-checkout 文件以包含所有内容,只需指定 / * ,使用 git read-tree -mu HEAD 更新您的工作树,然后在您的配置文件中禁用稀疏检出。


    I have some object files and assembly files that I have committed for the first version. I don't want to commit changes to these files as I make changes to my project and update the other source files. However, I do want to keep them in my repository, in the first commit.

    The two options I know of are:

    1. Remove them from the repository:1
      a. Update .gitignore
      b. git rm --cached <file> or...

    2. git update-index --assume-unchanged <file>2

    Both of these can be seen in this question: Ignore files that have already been committed to a Git repository

    and here:

    Git: ignore specific file(s) both on Commit and Pull


    1 ...However, I'd like them to stay within the repository and they will be removed when the next commit is completed.

    2 ...This option, as per this answer from this question notes:

    Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow... Especially, it is not a promise by Git that Git will always consider these paths are unmodified... it reserves the right to report that the path has been modified (as a result, "git commit -a" is free to commit that change).

    So is there a proper way to commit those files once, keep them in that state in the repository, and then ignore any further changes to them?

    I did find some information here: https://gist.github.com/canton7/1423106

    But it seems... involved. Is there no actual option that git contains that works as though --assume-unchanged does, but without lying to git?

    解决方案

    As of Git 1.7.0 you have a third option: sparse checkout.

    With sparse checkout you basically tell Git to exlude a certain set of files from the working tree. Those files will still be part of the repository but they won't show up in your working directory.

    Internally, sparse checkout uses the skip-worktree flag to mark all the excluded files as always updated. From the documentation:

    When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

    Here's how you enable sparse checkout in an existing repository:

    1. Enable sparse checkout in your repository with git config core.sparseCheckout true
    2. Create a .git/info/sparse-checkout file containing the paths to include in the working directory. In your case, you would have to include everything and then exclude the specific file by adding a ! in front of its path:

      /* !path-to-the-file-to-exclude

    3. Update your working directory with git read-tree -mu HEAD

    Note that if you want to get back the excluded file, it's not enough to simply disable sparse checkout with git config core.sparseCheckout false. You'll first have to modify the .git/info/sparse-checkout file to include everything by only specifying /*, update your working tree with git read-tree -mu HEAD and then disable sparse checkout in your configuration file.

    这篇关于将文件一次包含到存储库然后忽略后续更改的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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