简体   繁体   中英

Ignore tracked file changes

I have a file config.txt that is tracked by git, it is downloaded on git clone , but I don't want anybody changing and committing it ie if someone changes that file I don't want their changes to be tracked thus committed & accidentally pushed.

I still want to be able to add the potential changes of config.txt later on, but in an explicit way(could be git add -f config.txt or something else).

Also an extra step for doing this is not desirable so git update-index --skip-worktree or git update-index --assume-unchanged is not a good enough solution as it is prone to human error.

Is it possible to do so in git?

You cannot ignore changes to a tracked files in Git. The Git FAQ explains :

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.

If your goal is to work with a configuration file, then the best thing to do is add an example or template file and then either have the user copy it into place or have a script create the appropriate file. You should then ignore the location of the actual configuration file and only check in the example or the template.

One possible solution to that problem is to "skip" tracking the file after the repository is cloned:

git update-index --skip-worktree <path-name>

The downside is that this requires an additional step from all users after cloning the repo, so you could add that in your README

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