Ignore changes to a tracked file

佐手、 提交于 2019-11-29 19:11:58

问题


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?


回答1:


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.




回答2:


Since in one of your comments you are working directly in the master branch, the real problem to fix is working in master.

You'll want to create a Topic Branch in Git:

git checkout -b some_feature_or_bug_fix master

Then make the necessary changes to the config file, and commit as normal.

The nice thing here is you don't affect files in master. If you need to pull in updated code, just git fetch and git merge origin/master into your topic branch. Then you can deal with upstream changes to the config file as normal Git merges.

Prior to merging your topic branch into master you can do an interactive squash to reduce your commits and merge commits into one nice, clean commit. Or do a git merge --squash to merge your topic branch into master.



来源:https://stackoverflow.com/questions/32251037/ignore-changes-to-a-tracked-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!