Is it possible to stop tracking local changes to a

2020-03-26 08:39发布

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条回答
淡お忘
2楼-- · 2020-03-26 09:09

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)

https://git-scm.com/book/en/v2/images/smudge.png
(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.

查看更多
登录 后发表回答