Home > Software engineering >  git ignore file without deleting it on remote repository for all users
git ignore file without deleting it on remote repository for all users

Time:06-29

I saw many questions regarding this issue but I didn't saw appropriate solution for this problem. lets say that I have a template.xml file in remote and I want all users to get it when they first cloning the repository but when they changing it will be ignored. I assume-unchanged but it is only locally and I would like to have a solution for all users

CodePudding user response:

Your question is a specific case of "How do I ignore tracked files in Git?", which is answered in the Git FAQ:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

The FAQ also mentions an approach for template or configuration files:

[I]t can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.

You can use some sort of setup script to copy it into place if it's missing.

CodePudding user response:

If I understand your problem correctly, you could commit and push the template.xml file to your remote and then add a .gitignore file. The template.xml file will then be available on the remote for all, and if you clone the repository you will also get the file. But if you change the file locally it will not be recognized by git.

Basically after you add the .gitignore file you can't edit the file on the remote anymore.

gitignore file docs: https://git-scm.com/docs/gitignore

  •  Tags:  
  • git
  • Related