How can I make git ignore future revisions to a file?

前端 未结 7 1794
盖世英雄少女心
盖世英雄少女心 2020-11-28 18:31

I have created a default version of a file included in a git repository. It\'s important that when someone clones the repository, they get a copy of this file. However, I

相关标签:
7条回答
  • 2020-11-28 18:47

    Take a look at smudge/clean scripting. This way you can version control the file but when it is checked out, you will "smudge" it by replacing the generic/place-holder data with machine specific data in the file.

    When you commit it, you will "clean" it by replacing the machine specific information with generic or place-holder information.

    Smudge/clean scripts must be deterministic in that applying them multiple times, in different orders will be the equivalent of just running the last one in the sequence.

    The same can be applied with passwords if you need to expose your repository but the contents may contain sensitive information.

    0 讨论(0)
  • 2020-11-28 18:49

    As many others have mentioned, a good modern solution is:

    git update-index --skip-worktree default_values.txt
    

    That will ignore changes to that file, both local and upstream, until you decide to allow them again with:

    git update-index --no-skip-worktree default_values.txt
    

    You can get a list of files that are marked skipped with:

    git ls-files -v . | grep ^S
    

    Note that unlike --skip-worktree, the --assume-unchanged status will get lost once an upstream change is pulled.

    0 讨论(0)
  • 2020-11-28 18:56

    What you are searching for is git update-index --assume-unchanged default_values.txt.

    See the docs for more details: http://www.kernel.org/pub/software/scm/git/docs/git-update-index.html

    0 讨论(0)
  • 2020-11-28 18:56

    I've solved this by defining the "clean" filter to simply cat the contents of the file in the index.

    git show :path/to/myfile should just print the contents of the index for the specified file, so we can use that in a script to replace the working copy with the untouched copy in the index:

    #! /bin/sh
    
    git show :$1
    

    Set that as the "clean" filter for the file concerned (assuming you've put that in "discard_changes"):

    $ git config filter.ignore_myfile.clean "discard_changes path/to/myfile"
    $ echo "path/to/myfile filter=ignore_myfile" >> .gitattributes
    

    Unfortunately I can't find a way to make this generalisable to multiple files, as there's no way to tell which file we're processing from inside the clean script. Of course, there's nothing to stop you from adding a different filter rule for each file, but it's a bit cludgy.

    0 讨论(0)
  • 2020-11-28 18:58

    The approach I've generally seen is to create a file with a different name, eg: default_values_template.txt and put default_values.txt in your .gitignore. Instruct people to copy default_values_template.txt to default_values.txt in their local workspaces and make changes as needed.

    0 讨论(0)
  • 2020-11-28 19:00

    I suggest looking into submodules. If you place the machine specific files in a submodule, git add should ignore it.

    0 讨论(0)
提交回复
热议问题