2

Is there a way to make git ignore the contents of a file?

I have a file that is automatically overwritten by the compiler every time the build process runs, but the compiler fails if the file doesn't exist. Even if the file is completely empty the compiler is still happy. There is no point in updating this file every time I commit in git because its just going to get overwritten anyway.

Paul Rabahy
  • 79
  • 2
  • 8
  • 1
    Is it possible to just not check in the file at all and do something like `touch /path/to/file` as a precompile step? – vcsjones Sep 12 '16 at 20:34
  • 2
    https://duckduckgo.com/?q=git+ignore – Quentin Sep 12 '16 at 20:34
  • 1
    @Quentin It's not clear, but I think the OP wants the file to exist (empty) in their repo but _not_ be marked as dirty. That's not the same as the usual ignore. – Dan Puzey Sep 12 '16 at 20:35
  • Possible duplicate of [Tell git never to update a file](http://stackoverflow.com/questions/5658449/tell-git-never-to-update-a-file) – Scott Weldon Sep 12 '16 at 20:35
  • `--assume-unchanged` is kind of a dirty work around since that doesn't communicate back to the remote repository, so every time you check out from origin you need to run `--assume-unchanged` again. – vcsjones Sep 12 '16 at 20:36
  • @vcsjones Unfortunately not really. It is a proprietary IDE that has the compiler hooked right in. I guess I could do something like that as a smudge/clean filter, but that feels a bit ugly. – Paul Rabahy Sep 12 '16 at 20:37
  • @DanPuzey Correct. If the file doesn't exist, it breaks the compiler. – Paul Rabahy Sep 12 '16 at 20:38
  • @vcsjones I read about --assume-unchanged, but it appears that that only effects the local repo. We are using git as part of our deployment tooling (ugly I know), and I need this change in all the clones. – Paul Rabahy Sep 12 '16 at 20:43
  • @ScottWeldon Looks like there is some good advice in there. I might be able to do something like the answer there, but I was hoping to avoid writing more scripts if git could handle it on its own. – Paul Rabahy Sep 12 '16 at 20:45

2 Answers2

2

There's no point in checking such a file in at all. There are two things you need to do here:

  1. From Git's side, you need to add this file to .gitignore, so git just ignores it completely.

  2. From the build's side, you need to make sure the file is there (e.g., on a *nix system, just touch it) before you start the compilation.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I would love to do that, but it is a proprietary IDE/build system so I don't have any (easy) way to hook the build system to insert the `touch` commands. – Paul Rabahy Sep 12 '16 at 20:47
2

No, I am not aware of a way to commit a file and ignore its contents subsequently. However, why not add the file .gitignore, i.e. leave it untracked by git and cause git to create the file when the repository is cloned and/or checked out. This can simply be done by placing the following script inside ".git/hooks/post-checkout", remembering to make the file executable (and of course remember to git add the file so it is checked into the repository!).

#!/bin/sh

# Create file "myfile" in checkout root.
touch myfile

Which would create the file "myfile" if it does not exist already. And if the file is in the .gitignore it will not be committed and generally be ignored and not clutter up things like running "git status", etc.

I think this elegantly solves your problem.