从Git存储库中删除文件,而不从本地文件系统中删除该文件

Remove a file from a Git repository without deleting it from the local filesystem

我的初始提交包含一些日志文件。我已经将*log添加到我的.gitignore中,现在我想从我的存储库中删除日志文件。

1
git rm mylogfile.log

将从存储库中删除文件,但也将从本地文件系统中删除该文件。

如何在不删除本地副本的情况下从repo中删除此文件?


从你的文件:

When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

因此,一个单一的文件:

1
git rm --cached mylogfile.log

与一个单一的目录:

1
git rm --cached -r mydirectory


删除整个文件夹从回购(样resharper文件),这样做:

1
git rm -r --cached folderName

我已经通过一些resharper文件,和那些不想坚持到用户的其他项目。


所以,你可以删除的文件从您的代码库,基于.gitignore他们从本地文件系统:

1
git rm --cached `git ls-files -i -X .gitignore`

或者,也在Windows PowerShell,:

1
git rm --cached $(git ls-files -i -X .gitignore)


我的答案是:通过在stackoverflow.com http://///6313126问题如何删除A目录中的GitHub库

删除文件夹或文件/目录的只读Git仓库不是从当地的尝试3简单的步骤。

步骤删除目录

1
2
3
git rm -r --cached File-or-FolderName
git commit -m"Removed folder from repository"
git push origin master

下一步骤是在提交的文件夹记录

To ignore that folder from next commits make one file in root named .gitignore
and put that folders name into it. You can put as many as you want

.gitignore文件将看起来像这样

1
/FolderName

remove directory


所以,如果你有commited敏感数据(例如文件的密码,你应该含)完全删除它从历史的仓库。这是一个指南解释如何做:http://help.github.com /删除敏感数据


更通用的解决方案。

  • .gitignore文件编辑。

    ECHO mylogfile.log >> .gitignore

  • 删除所有的项目和指标。

    git rm -r -f --cached .

  • 重建索引。

    git add .

  • 新提交的化妆

    git commit -m"Removed mylogfile.log"


  • Git lets you ignore those files by assuming they are unchanged. This
    is done by running the git update-index --assume-unchanged
    path/to/file.txt
    command. Once marking a file as such, git will
    completely ignore any changes on that file; they will not show up when
    running git status or git diff, nor will they ever be committed.

    (从https://help.github.com /文章/忽略文件)

    因此,需要通过您的忽略它,但它永远的改变了。我认为这只是局部的作品,共工人仍然可以改变它,除非他们海上运行相同的命令或以上。(这是仍然需要验证。

    注:这不是直接回答问题,但问题是基于后续评论的其他答案。


    如果你想只是untrack a文件和不删除从本地和远程命令:使用thhis回购。

    1
    git update-index --assume-unchanged  file_name_with_path

    以上答案不工作对我来说。我过去filter-branch通过删除所有文件。

    删除一个文件从一个git版本库:

    1
    git filter-branch --tree-filter 'rm  file'

    删除一个文件夹从一个git版本库:

    1
    git filter-branch --tree-filter 'rm -rf directory'

    本removes目录或文件从所有的承诺。

    你可以通过使用指定的提交:a

    1
    git filter-branch --tree-filter 'rm -rf directory' HEAD

    或范围:

    1
    git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD

    一个推到远程的一切,你可以做的:

    1
    git push origin master --force