Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

git - Ignore changes to a tracked file

I want to change a tracked file for development only, but keep the tracked version unchanged.

Most "solutions" for this suggest

git update-index --assume-unchanged

but it's totally useless, because the file will still be changed by checkout or reset. Is there a better solution that survives checkout and reset commands?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Quick Fix

This is what I think you're trying to do, change a file, but ignore it when committing.

git update-index --skip-worktree my-file

Here is a good answer regarding the difference between assume-unchanged and skip-worktree.

Git will still warn if you try to merge changes into my-file. Then you will have to "unskip" the file, merge it and "re-skip" it.

git update-index --no-skip-worktree my-file
# merge here
git update-index --skip-worktree my-file

There can also be problems if you modify the file, then switch to a branch where that file has been changed. You may have to do some fancy "skip/unskip" operations to get around that.

Long Term Fix

In the long term, you probably want to separate your "local" changes into a second file. For example, if the file you want to change is a config file, create a "default" config file that you check into the repository. Then, allow a second "overrides" config file that is optional and put that file in your .gitignore.

Then, in your application, read the default config file and then check if the overrides file exists. If it does, merge that data with the data from the default file.

This example is for a config file, but you can use that technique for other kinds of overrides if needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...