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

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

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

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

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


当前回答

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

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

其他回答

忘了你的gitignore?

如果您在本地拥有整个项目,但忘记添加git-ignore,现在正在跟踪一些不必要的文件,请使用此命令删除所有内容

git rm --cached -r .

确保您处于项目的根位置。

然后你可以像往常一样

Add

git add .

犯罪

git commit -m 'removed all and added with git ignore'

Push

git push origin master

结论

希望这能帮助那些不得不对自己的.git进行更改的人。

它会删除整个缓存看看你的.gitignore添加要跟踪的文件推送至您的回购

git rm--文件名

gitls文件,以确保文件已删除或未跟踪

gitcommit-m“取消跟踪更改”

数字推送

要告诉Git不要跟踪对本地文件/文件夹的更改(这意味着Git状态不会检测到对它的更改),请执行以下操作:

git update-index --skip-worktree path/to/file

要让Git再次跟踪本地版本的更改(以便您提交更改),请执行以下操作:

git update-index --no-skip-worktree path/to/file

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

在下面,“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状态的输出中看到这些文件。

请注意,此解决方案从存储库中删除文件,因此所有开发人员都需要维护自己的本地(非版本控制)文件副本

要防止git检测到这些文件中的更改,还应使用以下命令:

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

你可能想做什么:(从下面@Ryan Taylor回答)

这是为了告诉git您想要自己的独立版本的文件或文件夹。例如,您不想覆盖(或删除)生产/登台配置文件。git更新索引--跳过工作树<路径名>

完整答案在以下URL中:http://source.kohlerville.com/2009/02/untrack-files-in-git/