Domanda

For some reason, I would like to have an empty file on my repository. The file needs to exists, but the build process changes it, and I do not want those changes to be committed. I need that users that clone my repository ignore changes on this file too.

git update-index --assume-unchanged seems good, but it only works locally.

Do you know how I can solve this ?

È stato utile?

Soluzione

The clean filter approach mentioned by jthill is the right approach. +1

git clean filter

It forces the file to be restored each time it is "added" to the index (meaning it never is actually added, since it hasn't changed).

Problem: How to force users to define it?

Solution: make them clone a template repo, which already contains:

  • that empty file,
  • and the .gitattributes file with the clean filter declared in it

If you set your main centralized repo to deny a ref update which is not a fast-forward one (git config receive.denyNonFastForwards true), your users won't have any choice but to push from a repo with the same common history than your template repo.
That means they will have those settings in place.

Sure they could delete said settings in a new commit, but that is when the pre-receive hooks from jthill's answer kicks in.
If they don't need to know about that files, chances are they won't mess with what was already defined for them.

Altri suggerimenti

It's easy to set up a repo to preserve a particular committed state for a file, and you can reject anything inbound you don't like.

To preserve a particular committed state for a file:

# do this once when the committed path/to/file is in the state you want
x=`git rev-parse master:path/to/file`
echo path/to/file filter=wired-to-$x >>.gitattributes

# then in each repo that wants it
git config filter.wired-to-$x.clean "git show $x"

The pre-receive hook tests inbound pushes, this simple one

#!/bin/sh
set -e
while read old new ref; do
        git ls-tree $new path/to/file \
        | awk '$3 != "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" {
                print "path/to/file is not empty"; exit 1; }'
done

will reject any push containing an inbound branch tip with a non-empty path/to/file. Sub in your "$x" to check for your desired state, and for added thoroughness you can run through all the inbounds with an inner rev-list loop feeding the ls-tree.

If they do not git add the changed file then it's not a problem. Teach them not to use git add . and it's near equivalents Difference of “git add -A” and “git add .”.

The project README could also include your git update-index --assume-unchanged instruction for the relevant file(s)

This seems like an application's task rather than git's. I would add this file to .gitignore and make sure that build process or any other process that uses it could create it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top