40

I need to ignore files in git! I don't think ignore is the right word actually; I want the files to be in git, but I don't want to be able to commit changes to them. This may seem like a weird thing to some, but there are many, many instances that I need this functionality. The one instance I need it for now is when using git with a CMS; the CMS won't work without files that it is constantly changing, but I don't want those files to ever be committed after the initial commit. (This is very easy to do with SVN and Tortoise).

WorkFlow:

  1. Get All Files needed to run the app.
  2. Ignore specified directories / files when committing.

Here is what I've tried:

  1. .gitignore -- the files never enter git. If the file is already in cache the .gitignore file doesn't do anything.
  2. /.git/info/exclude -- same problem as .gitignore, but only for the local repo.
  3. Branching -- master => LocalIgnores => WorkingBranch. When the Working branch is merged with master, the changes made from LocalIgnores end up in master. Also, when you checkout one of the new branches, the deleted files get deleted instead of ignored.
  4. Thirdparty File Structure -- Thirdparty directory on the root node that holds a copy of all important thirdparty files so they can be copied into the working directory that utilizes the .gitignore file. (This one works, but there has to be an easier / better solurtion).
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Patrick
  • 409
  • 1
  • 4
  • 3

2 Answers2

96

Here is my answer from a similar question.

git update-index should do what you want

This will tell git you want to start ignoring the changes to the file
git update-index --assume-unchanged path/to/file

When you want to start keeping track again
git update-index --no-assume-unchanged path/to/file

Community
  • 1
  • 1
Andy
  • 44,610
  • 13
  • 70
  • 69
  • Awesome. That did it. I created a couple of bat files, IgnoreLocally.bat and UnIgnoreLocally.bat, that run from a .LocalIgnore file. One click is much easier than copy&paste tons of files. Thanks For the help. Now to get tortoise to include it into their stuff! – Patrick Aug 15 '11 at 21:06
  • 3
    Why don't you close it as a duplicate, rather than quoting from your answer? There are so many questions on this very thing. It all just becomes noise for future users. – manojlds Aug 15 '11 at 21:28
  • @manojlds I didn't know there was a way to flag a duplicate... Are you supposed to flag: needs attention->other->than write in "duplicate"? – Andy Aug 15 '11 at 21:32
  • @Andy - There is a `close` next to left of the flag. Choose Exact Duplicate in that. – manojlds Aug 15 '11 at 21:34
  • @manojlds I don't have that [privilege](http://stackoverflow.com/privileges/close-questions) yet. – Andy Aug 15 '11 at 21:39
  • @Andy - I am sorry, but it is pretty bad that some one with 1.5k rep can't vote to close! – manojlds Aug 15 '11 at 21:41
  • @Andy - you could still do the Alternatives to closing. Also, I have already voted to close and you will see a comment with a link to the duplicate post. – manojlds Aug 15 '11 at 21:42
  • Actually, don't close this yet... This isn't a very good solution; rather it is incomplete. There has to be a way to avoid running this every time you switch between branches, pull, etc. – Patrick Aug 16 '11 at 14:48
3

Another solution is to use a pre-commit hook. See git help hooks for details.

  • This is harder to set up: you have to write a shell script to express exactly what you want to allow or disallow, which probably means you need to have a fairly good grasp of git.

  • It's more permanent and flexible: with tricks like git update-index you can accidentally forget to run it, etc. But once you set up a hook, you never have to worry about it again.

Like the other tricks suggested, hooks don't automatically get propagated between repositories, but you can check them in as regular files and have everyone link them into .git/hooks.

wjl
  • 7,519
  • 2
  • 32
  • 41