我克隆了一个包含一些.csproj文件的项目。我不需要/喜欢我的本地csproj文件被Git跟踪(或在创建补丁时被提起),但很明显,项目中需要这些文件。

我已经将*.csproj添加到我的LOCAL.gitignore中,但文件已经在repo中。

当我键入gitstatus时,它显示了我对csproj的更改,我对跟踪或提交补丁程序不感兴趣。

如何从我的个人存储库中删除这些文件的“跟踪”(但将它们保存在源中,以便我可以使用它们),以便我在执行状态(或创建补丁)时不会看到更改?

是否有正确/规范的方法来处理这种情况?


当前回答

防止git监视文件或路径

git update-index --assume-unchanged [file-path]

并将其恢复使用

git update-index --no-assume-unchanged [file-path]

参考类似用例的回购https://github.com/awslabs/git-secrets

其他回答

为了忽略对目录中所有文件(特定类型)的任何更改,我必须结合这些方法,否则如果文件以前不存在,就会创建这些文件。

在下面,“excludedir”是我不希望看到更改的目录的名称。

首先,从更改跟踪缓存中删除任何现有的新文件(而不从文件系统中删除)。

git status | grep "new file:" | cut  --complement -d " " -f1-4 | grep "^excludedir" | xargs git rm --cache

您可以使用modified:执行相同操作。重命名:有点复杂,因为您必须查看新文件名的post->位,然后按照下面对delete:的描述执行pre->位。

deleted:文件证明有点复杂,因为您似乎无法更新本地系统上不存在的文件的索引

echo .deletedfiles >> .gitignore
git status | grep "deleted:" | cut  --complement -d " " -f1-4 | grep "^excludedir" > .deletedfiles
cat .deletedfiles | xargs -d '\n' touch
cat .deletedfiles | xargs -d '\n' git add -f
cat .deletedfiles | xargs -d '\n' git update-index --assume-unchanged
cat .deletedfiles | xargs -d '\n' rm

上面列表中的最后一个命令将再次从您的文件系统中删除文件,因此请随意省略。

然后,阻止该目录中的更改跟踪

git ls-files excludedir/ | xargs git update-index --skip-worktree
git update index --skip-worktree excludedir/

这是一个两步过程:

删除对文件/文件夹的跟踪-但将其保留在磁盘上-使用git rm—缓存现在,它们没有显示为“已更改”,但仍显示为git状态为-u的未跟踪文件将它们添加到.gitignore

我假设你试图从gittacking中删除一个文件。为此,我建议使用以下命令。

git更新索引--假设未更改

Ex-git更新索引——假设没有变化。gitignore.deide/compiler.xml

我假设您正在询问如何删除特定文件夹或bin文件夹中的所有文件,而不是单独选择每个文件。

您可以使用以下命令:git rm-r-f/<floder名称>\*确保您位于该目录的父目录中。此命令将递归地“删除”bin/或build/文件夹中的所有文件。所谓delete,我的意思是git将假装这些文件被“删除”,而这些文件将不会被跟踪。git确实将这些文件标记为处于删除模式。

请确保您的.gitignore已准备好迎接即将到来的提交。文档:git rm

将.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

如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从将来的拉取中删除新忽略的文档,请参阅此答案。