开发者

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

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 would like to set git so that it ignores changes to this file later. .gitignore works only on untracked files.

My motivation is that this file contains machine-specific information. I would like to provide default values, while allowing people to make local changes that won't get pushed back to the origin repository, creating merge conflicts when we pull new changes.

We are generally pre开发者_运维技巧tty lazy and use git add . a lot, so I'm pretty sure if I can't tell git to ignore this file, changes to it will end up getting committed and pushed.

To summarize,

  1. I would like to create a file, call it default_values.txt that is added to my git repository and is included when someone clones that repository.
  2. git add . should not add default_values.txt to the commit.
  3. This behavior should be passed on to any clones of the repository.


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.


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


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.


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.


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.


I found a solution that works for my team. We share our githooks via symlinks and after adding a template file to git I added a pre-commit hook that checks whether the template file has been modified and if so I git reset -- templatefile.txt. If it's the only changed file I also abort the commit.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜