Is it possible to stop tracking local changes to a file that you *do* want to be pulled down if changed in the repo?

你说的曾经没有我的故事 提交于 2019-12-01 06:53:33

问题


We have a config file in the repo which all the users contribute to based on their function. For my local testing, I need to manually change two values in that config file, but I never want my changes committed back to the server.

I do however want to pull updates to the file if there's a newer version in the repo, even if that means my local changes will be overwritten. That's fine, I can just make them again from a stash I created specifically to do exactly that.

What I'm wondering is if I can tell Git "Hey... I will never push back my local changes to the server, so don't show it to me as changed, but if you have new changes for me, lemme have them when I pull!"

Here's an example of what I'm after.

Server: VersionA
Local:  [Nothing]

(I do a Git Pull)

Server: VersionA
Local:  VersionA

(I make local changes)

Server: VersionA
Local:  VersionA (Modified)

(What I want...)

Server: VersionA
Local:  VersionA (Modified, but ignored locally by Git)

(Someone checks in an update to the file)    

Server: VersionB
Local:  VersionA (Modified, but local changes ignored by Git)

(I do a git Pull)

Server: VersionB
Local:  VersionB (This overwrites my local, modified VersionA)

(I then manually update VersionB with my stashed changes)

Server: VersionB
Local:  VersionB (modified, but local changes ignored by Git)

The .gitignore file doesn't apply here because that stops new files from being added, but again, this file has already been added. I just want it to always ignore my local changes, as if they weren't there. I only care if the file has changed on the server.

So can Git be configured to do this?


回答1:


The .gitignore file doesn't apply here because that stops new files from being added, but again, this file has already been added.
I just want it to always ignore my local changes.

Another approach would be to make your modifications... in a private file, one which is not tracked and is ignored.

Meaning all the common contributions are done in a template file., used to generate the actual file (as a private non-versioned file, added to your .gitignore).

That generation would be automatic on git checkout.

First, rename your existing file:

git mv aConfigFile aConfigFile.tpl
git commit -m "record template config file"

Then add to aConfigFile to a .gitignore file: a git status won't show it anymore to be added/changed.

Now, declare a smudge content filter driver which, automatically, will re-generate that file (ignored, since it is in .gitignore)


(image from "Customizing Git - Git Attributes", from "Pro Git book")

smudge script (which you can version): YourScript

copy aConfigFile.tpl aConfigFile

Declare the content filter driver in a versioned .gitattributes:

echo 'aConfigFile.tpl config' >> .gitattributes

That smudge 'config' content filter driver needs to be activated locally by each user cloning that repo.

cd /path/to/repo
git config filter.config.smudge YourScript

That last step is the only one each user need to do in order to benefit from the actual config file to be automatically generated on each git checkout.

Any modification done on the generated file will remain local.



来源:https://stackoverflow.com/questions/45617751/is-it-possible-to-stop-tracking-local-changes-to-a-file-that-you-do-want-to-be

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