我克隆了一个包含一些.csproj文件的项目。我不需要/喜欢我的本地csproj文件被Git跟踪(或在创建补丁时被提起),但很明显,项目中需要这些文件。
我已经将*.csproj添加到我的LOCAL.gitignore中,但文件已经在repo中。
当我键入gitstatus时,它显示了我对csproj的更改,我对跟踪或提交补丁程序不感兴趣。
如何从我的个人存储库中删除这些文件的“跟踪”(但将它们保存在源中,以便我可以使用它们),以便我在执行状态(或创建补丁)时不会看到更改?
是否有正确/规范的方法来处理这种情况?
正如其他答案所指出的,所选答案是错误的。
另一个问题的答案表明,可能需要跳过工作树。
git update-index --skip-worktree <file>
这个答案链接到一篇文章(http://fallengamer.livejournal.com/93321.html)并引用了这篇文章,很好地概括了--假设不变和--跳过工作树之间的区别,如下所示:
--假设未更改假设开发人员不应该更改文件。此标志用于提高不更改文件夹的性能如SDK。
--当您指示git不要触摸特定文件,因为开发人员应该更改它时,skip-worktree非常有用。例如,如果上游的主存储库承载一些生产就绪配置文件,您不想意外提交更改对于这些文件,跳过工作树正是您想要的。
所有这些都归功于borealid的研究和回答。此处包含此信息纯粹是为了方便他人。
将.gitignore应用于当前/未来
此方法应用标准的.gitignore行为,不需要手动指定需要忽略的文件。无法再使用--exclude-from=.gitignore:/-以下是更新的方法:一般建议:从一个干净的repo开始-所有内容都已提交,工作目录或索引中没有挂起的内容,并进行备份!
#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"
#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *
#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
git commit -m "re-applied modified .gitignore"
#other devs who pull after this commit is pushed will see the newly-.gitignored files DELETED
如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从将来的拉取中删除新忽略的文档,请参阅此答案。