使用 gitignore 清理 Git 存储库
Clean Git Repository using gitignore
所以我在 bitbucket 上有这个 C#
两年前的回购协议。直到昨天才 .gitignore
- 我知道,晚了。
这是个人项目。
我可以使用 .gitignore
作为基础清理存储库吗?所以工作人员会有效地删除 .gitignore
说不应该存在的任何内容?
您需要在本地克隆项目(如果您还没有),然后手动删除文件或文件夹(不应再存在,即 gitignore 文件中提到的那些),然后提交那些删除。然后你可以将它推送到你的远程仓库。
我想你想要的是这个:
要取消跟踪已 added/initialized 到您的存储库的 单个 文件,即 ,请停止跟踪该文件,但不要从您的系统中删除它使用:git rm --cached filename
取消跟踪 现在在您 .gitignore
中的每个 文件:
首先提交任何未完成的代码更改,然后,运行此命令:
git rm -r --cached .
这会从 index(暂存区)中删除任何更改的文件,然后只是 运行:
git add .
提交:
git commit -m ".gitignore is now working"
Notice: First of all you better take a backup of your git repository, since it always can go wrong badly (the exact behavior of git
can depend on all kinds of settings/... so it is not completely predictable what the result of the process will be, so better be safe than sorry). It is sufficient to copy the directory to some other location: there is a (hidden) .git
directory in that directory containing the history, tags, etc.
仅最后一次提交
如果您只想删除新提交的内容,那么您不关心存储在文件系统历史记录中的文件,您可以使用以下方法删除这些文件:
git rm --cached -r .
git add .
rm --cached -r .
表示您将从 git 存储库中删除所有内容,但将文件保留在文件系统中。您以递归方式执行此操作,因此 none 文件将不再是存储库的一部分。接下来,您再次将所有文件添加到存储库,但是 .gitignore
文件将不会重新添加那些应该被忽略的文件。
所有提交(整个历史)
如果您想从所有以前的提交中删除这些文件,我建议使用 bash 脚本。它可能不是最有效的。
现在我们可以做的是列出所有应该被忽略至少一次的文件:
git ls-files --exclude-standard --others --directory --ignored
我会首先检查列表 并检查是否没有应该保留的文件。相应地更新 .gitignore
,仅当这些文件中的 none 个再次出现时才转到下一步。
如果列表中没有更多文件要保留在存储库中,您可以运行此脚本:
#!/bin/bash
for f in `git ls-files --exclude-standard --others --directory --ignored`
do
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch '$f'" --prune-empty --tag-name-filter cat -- --all
done
该脚本在历史记录中至少迭代一次要忽略的文件,并通过所有提交执行文件过滤。请注意,这些文件并未从您的文件系统中删除。但是您将丢失这些文件的历史记录。
此外,对于标准 .gitignore
s(例如可以在 GitHub 找到)必须小心一些,因为我听说 white/blacklisted 之间存在差异在文件系统上 运行ning Windows.
一些提交可能会消失,因为这些提交的唯一区别涉及不再是存储库一部分的文件。例如,如果您忽略 *.log
文件并且日志包含日期;可能只有 file.log
文件被修改了。如果删除该文件,修改该文件的提交也将被删除。
所以我在 bitbucket 上有这个 C#
两年前的回购协议。直到昨天才 .gitignore
- 我知道,晚了。
这是个人项目。
我可以使用 .gitignore
作为基础清理存储库吗?所以工作人员会有效地删除 .gitignore
说不应该存在的任何内容?
您需要在本地克隆项目(如果您还没有),然后手动删除文件或文件夹(不应再存在,即 gitignore 文件中提到的那些),然后提交那些删除。然后你可以将它推送到你的远程仓库。
我想你想要的是这个:
要取消跟踪已 added/initialized 到您的存储库的 单个 文件,即 ,请停止跟踪该文件,但不要从您的系统中删除它使用:git rm --cached filename
取消跟踪 现在在您 .gitignore
中的每个 文件:
首先提交任何未完成的代码更改,然后,运行此命令:
git rm -r --cached .
这会从 index(暂存区)中删除任何更改的文件,然后只是 运行:
git add .
提交:
git commit -m ".gitignore is now working"
Notice: First of all you better take a backup of your git repository, since it always can go wrong badly (the exact behavior of
git
can depend on all kinds of settings/... so it is not completely predictable what the result of the process will be, so better be safe than sorry). It is sufficient to copy the directory to some other location: there is a (hidden).git
directory in that directory containing the history, tags, etc.
仅最后一次提交
如果您只想删除新提交的内容,那么您不关心存储在文件系统历史记录中的文件,您可以使用以下方法删除这些文件:
git rm --cached -r .
git add .
rm --cached -r .
表示您将从 git 存储库中删除所有内容,但将文件保留在文件系统中。您以递归方式执行此操作,因此 none 文件将不再是存储库的一部分。接下来,您再次将所有文件添加到存储库,但是 .gitignore
文件将不会重新添加那些应该被忽略的文件。
所有提交(整个历史)
如果您想从所有以前的提交中删除这些文件,我建议使用 bash 脚本。它可能不是最有效的。
现在我们可以做的是列出所有应该被忽略至少一次的文件:
git ls-files --exclude-standard --others --directory --ignored
我会首先检查列表 并检查是否没有应该保留的文件。相应地更新 .gitignore
,仅当这些文件中的 none 个再次出现时才转到下一步。
如果列表中没有更多文件要保留在存储库中,您可以运行此脚本:
#!/bin/bash
for f in `git ls-files --exclude-standard --others --directory --ignored`
do
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch '$f'" --prune-empty --tag-name-filter cat -- --all
done
该脚本在历史记录中至少迭代一次要忽略的文件,并通过所有提交执行文件过滤。请注意,这些文件并未从您的文件系统中删除。但是您将丢失这些文件的历史记录。
此外,对于标准 .gitignore
s(例如可以在 GitHub 找到)必须小心一些,因为我听说 white/blacklisted 之间存在差异在文件系统上 运行ning Windows.
一些提交可能会消失,因为这些提交的唯一区别涉及不再是存储库一部分的文件。例如,如果您忽略 *.log
文件并且日志包含日期;可能只有 file.log
文件被修改了。如果删除该文件,修改该文件的提交也将被删除。