简体   繁体   中英

Keep file in git repository and ignore local edits/delete

I have a file which is in my git:

dir1/.thisfile

The content of .thisfile is "Hello" Now I want when users do a git clone they have this file. After that I want the user to delete the file or edit the content of the file locally but I want this to be ignored by git.

How do I achieve this? I have put the file in my .gitignore but when I delete it or change it it is staged. Gitignore seems only to work for files which aren't in my gitrepo.

I'm not exactly sure I fully understand you, but if you want to temporarily hide a file from being included in a commit you can use:

git update-index --assume-unchanged <file>

... and the change it back if/when needed with:

git update-index --no-assume-unchanged <file>

Hope this helps.

If you have the file in repo, no matter if it is mentioned in .gitigore . Every user cloning the repo will get the file but since it is listed on .gitigore they cannot commit it again unless it is removed from .gitigore by them.

So upload the file to repo and the add it to .gitigore . If you want to keep track of local file the check this question.

Generally speaking if you want the files to be ignored globally, they have to be added to global repo git.ignore . I don't see any in-built way of implementing your requirements but you can do it with the following method .

Before pushing the file dir1/.thisfile into your repo, change the name to for eg

dir1/.thisfile.model

or

dir1/.thisfile.example

or whatever you like, as long as it is different and meaningful.

Push the file, and then add the original filename (dir1/.thisfile) to global git.ignore . Now the users will always pull that file, but it will clearly be marked with a standout name. If they want to make "production" use out of it (whatever that would be), they need to locally change the name to the original. If they do, no worries, it is ignored already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM