简体   繁体   English

Git:忽略跟踪的文件

[英]Git: Ignore tracked files

I have some tracked files in a repository which are automatically modified when building the code.我在存储库中有一些跟踪文件,这些文件在构建代码时会自动修改。 I don't want to untrack them, I just don't want them to appear as modified and I don't want them to be staged when I git add.我不想取消跟踪它们,我只是不希望它们显示为已修改,并且我不希望它们在我添加 git 时上演。

Is this possible?这可能吗?

Sure. 当然。

git update-index --assume-unchanged [<file> ...]

To undo and start tracking again: 要撤消并再次开始跟踪:

git update-index --no-assume-unchanged [<file> ...]

An another solution using git attributes and %f in filter command: 在filter命令中使用git属性和%f的另一种解决方案:

git config filter.orig.clean "cat %f.orig"
cp filename filename.orig
echo "filename filter=orig" >> .git/info/attributes
echo "filename.orig" >> .git/info/exclude

Another approach (from a now deleted answer by Seth Robertson, but I found it helpful so resurrecting it) is to maintain a "tracked" template file, then have local untracked version of it, ex: "config.sample.ini" or "config.ini.template" see https://gist.github.com/canton7/1423106 for a full example. 另一种方法(来自Seth Robertson现在删除的答案,但我发现它有用,所以它有效复活)是维护一个“跟踪”的模板文件,然后有本地未跟踪的版本,例如:“config.sample.ini”或“ config.ini.template“请参阅https://gist.github.com/canton7/1423106以获取完整示例。

Then there won't be any concerns if the file is changed within git, etc. and you can use .gitignore (finally) on the local untracked files. 如果文件在git等中更改,则不会有任何顾虑,并且您可以在本地未跟踪文件上使用.gitignore(最终)。

The accepted answer is not correct.接受的答案不正确。 --assume-unchanged only causes Git to skip certain (sometimes expensive) file system checks -- it doesn't guarantee that Git shows the file as "unchanged". --assume-unchanged只会导致 Git 跳过某些(有时是昂贵的)文件系统检查——它不能保证 Git 将文件显示为“未更改”。

The same command but with the option --skip-worktree , however, does work.但是,带有选项--skip-worktree的相同命令确实有效。 So to prevent a tracked but changed file from appearing as changed in the Git status, use因此,为了防止跟踪但已更改的文件在 Git 状态中显示为已更改,请使用

git update-index --skip-worktree [<file> ...]

To undo and start showing it as changed again:要撤消并再次开始将其显示为已更改:

git update-index --no-skip-worktree [<file> ...]

See also the cited email from a Git maintainer in this answer , the Git documentation git-update-index , and FallenGameR's blog entry about how the two react to different scenarios.另请参阅此答案中来自 Git 维护者的引用 email ,Git 文档git-update-indexFallenGameR 的博客条目关于两种不同场景的反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM