简体   繁体   中英

Git ignore changes to file but keep it in the repo

This is a git question concerning our C# Xamarin project.

We have a couple files in an Assets folder that are copied in there by a pre-build event command. The files are not used directly by the compiler but if they are missing our CI server will fail the build. The versions of them that are on all the dev machines are probably different so whenever we build on our machines the files get copied in and git shows that they are changed

It's annoying because changes to these files really don't matter so we never need to commit them. I want to ignore changes to these files but not remove them from the repo. If they are removed as mentioned above CI will fail. If I straight up "remove and stop tracking" them they will be deleted from the CI server when it pulls.

From other stack overflow questions I have tried both of these:

git update-index --assume-unchanged [path]
git update-index --skip-worktree [path]

Both succeed in hiding the file changes but both present the following problem. When I try to switch branches it tells me I must discard unstaged changes before switching and lists those files. I then type:

git reset -- .
git checkout .

Both fail to discard the changes to the files until I reverse the update-index commands, only then can I discard the changes and be allowed to switch branches.

Is there a solution to this? Are we doing something outside the box or just plain wrong?

This is a very common problem.

Sometimes it's the well known custom.js , custom.min.js , custom.css , custom.min.css , and the likes. Other times it's another kind of "source files" that somehow needs to be "there" in order for software to work, but we dont really care about what's in them.

Sounds familiar? It's called compiled files. Now I know they are not actually compiled files - they are source files. But for all intents and puposes these files are pretty much the same as compiled files. They are not really anything you care about, and would rather the computer just figured out how to maintain them.

Why treat them any different, than actual compiled files? You mention a pre-build event command makes these files every time you build. That practically makes them part of the build process.

The solution is rather straightforward. On the CI server include the pre-build event that generates the files, before the actual compiling, and add the files to the .gitignore file.

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