记录一次清除git历史记录

  1. 步骤三: 清理和回收空间
 git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch -r 抓包' --prune-empty --tag-name-filter cat -- --all
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...Rewrite 7059304eae1be5aa2d34594d67b06082ce84c070 (37/63) (9 seconds passed, remaining 6 predicted)    rm '抓包/-01.cap'
rm '抓包/-01.csv'
rm '抓包/-01.kismet.csv'
rm '抓包/-01.kismet.netxml'
rm '抓包/-01.log.csv'
rm '抓包/-02.cap'
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch -r 抓包' --prune-empty --tag-name-filter cat -- --allWARNING: git-filter-branch has a glut of gotchas generating mangled history         rewrites.  Hit Ctrl-C before proceeding to abort, then use an         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...Cannot rewrite branches: You have unstaged changes.

git filter-branch 是一个强大的工具,用于重写 Git 存储库的历史记录,但它也有一些警告和潜在的陷阱,正如你所看到的那样。在你的情况下,你收到了一个警告,建议你使用 git filter-repo 作为替代方案,因为 git filter-branch 有时可能会导致复杂的历史记录重写。

另外,你遇到的错误消息“Cannot rewrite branches: You have unstaged changes”表明你有未提交的更改在你的工作目录中。git filter-branch 要求你的工作目录是干净的,以便它可以安全地重写历史记录。

为了解决这个问题,你可以按照以下步骤操作:

  1. 提交或丢弃你的未暂存更改。

    • 如果你想要保留这些更改,请使用 git add 暂存它们,然后使用 git commit 提交它们。
    • 如果你不想保留这些更改,可以使用 git stash 将它们保存为一个临时的“stash”,稍后可以再恢复,或者简单地使用 git checkout .git reset --hard(警告:这将丢弃所有未暂存的更改)来丢弃它们。
  2. 清理工作目录后,再次运行 git filter-branch 命令。

  3. 如果你想要忽略警告并继续使用 git filter-branch,可以设置环境变量 FILTER_BRANCH_SQUELCH_WARNING=1,但这并不推荐,除非你完全了解 git filter-branch 的潜在风险。

  4. 考虑使用 git filter-repo,这是一个更现代、更快、更易于使用的工具,用于重写 Git 历史记录。如果你决定使用 git filter-repo,你需要先安装它(通常通过 pip),然后按照它的文档进行操作。

注意:重写 Git 历史记录是一个破坏性的操作,并且可能会导致你的存储库与其他克隆或拉取请求的存储库不兼容。在进行此类操作之前,请确保你了解这些风险,并备份你的存储库。

git push origin master --force --all
fatal: --all can't be combined with refspecs

git push origin master --force --all 命令中的 --all 参数与特定的引用规范(refspec)master 不能一起使用,因为这两个选项有不同的目的和用法。

  • --all:这个选项告诉 Git 推送所有分支到远程仓库。
  • master:这是一个引用规范,指定了要推送的特定分支。
  • --force:这个选项(有时也写作 -f)用于强制推送,即使这会导致远程仓库上的历史记录被覆盖。

如果你想要强制推送 master 分支,你应该只使用 master 作为引用规范,并加上 --force 参数:

git push origin master --force

或者简写为:

git push -f origin master

如果你想要推送所有分支,并强制覆盖远程仓库上的分支(这是一个危险的操作,因为它会覆盖远程仓库上所有分支的历史记录),你应该使用 --all--force 一起:

git push --all --force origin

或者简写为:

git push -f --all origin

但是请注意,使用 --force--all 一起推送可能会对你的协作者造成问题,特别是如果他们也在推送更改到相同的分支。确保你了解这些操作的后果,并在团队中协调好这些操作。

git push origin main --force
Enumerating objects: 735, done.
Counting objects: 100% (732/732), done.
Delta compression using up to 12 threads
Compressing objects: 100% (594/594), done.
Writing objects:  52% (381/727), 53.70 MiB | 1.65 MiB/s

git push origin main --force 命令用于强制推送本地的 main 分支到远程的 origin 仓库,并且会覆盖远程仓库上的 main 分支。在你给出的输出中,Git 正在准备推送一系列对象(即你的更改)到远程仓库。

输出信息解释如下:

  • Enumerating objects: 735, done.:Git 正在计算需要推送的对象的数量,并发现总共有 735 个对象。
  • Counting objects: 100% (732/732), done.:Git 已经计算了所有 732 个对象(可能有些对象在压缩过程中会合并,所以最终数量可能与 735 不完全匹配)。
  • Delta compression using up to 12 threads:Git 正在使用多达 12 个线程来进行 delta 压缩,这是一种优化策略,可以减小推送数据的大小。
  • Compressing objects: 100% (594/594), done.:所有的对象(在这种情况下是 594 个)都已经被压缩。
  • Writing objects: 52% (381/727), 53.70 MiB | 1.65 MiB/s:Git 正在将压缩后的对象写入到传输协议中以便推送到远程仓库。当前进度是 52% 完成,已经写入了 381 个对象(总共 727 个),并且已经传输了 53.70 MiB 的数据,当前的传输速度是 1.65 MiB/s。

这个过程可能会持续一段时间,特别是如果你的网络连接速度较慢,或者需要推送的数据量很大。一旦完成,你的本地 main 分支的更改就会被推送到远程的 origin 仓库,并且覆盖远程仓库上的 main 分支。

注意:使用 --force 选项进行推送会覆盖远程仓库上的历史记录,这可能会导致其他人的工作丢失。在大多数情况下,最好先与团队成员协调,并确保他们知道你要进行强制推送。如果你正在单独工作,或者你确定需要覆盖远程仓库上的更改,那么使用 --force 选项是可以的。但在团队项目中,应该尽量避免使用它,除非有明确的理由。

步骤三: 清理和回收空间

虽然上面我们已经删除了文件, 但是我们的repo里面仍然保留了这些objects, 等待垃圾回收(GC), 所以我们要用命令彻底清除它, 并收回空间.

命令如下:

复制代码

$ rm -rf .git/refs/original/

$ git reflog expire --expire=now --all

$ git gc --prune=now
Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (1378/1378), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1461), reused 1802 (delta 1048)
$ git gc --aggressive --prune=now
Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (2426/2426), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1483), reused 0 (delta 0)

复制代码

注: 绿色字部分是命令执行后的结果.

git gc 命令用于执行 Git 仓库的垃圾收集(garbage collection),清理不需要的对象并优化本地仓库的性能。当你加上 --aggressive--prune=now 选项时,你正在指示 Git 执行更彻底和即时的清理。

具体来说:

  • --aggressive:这个选项会尝试更积极地清理和压缩仓库。它会进行更多的优化,包括更深入地查找可以清理的对象。但请注意,这可能会增加 git gc 的运行时间。
  • --prune=now:这个选项告诉 Git 立即执行“修剪”操作,删除所有不可达的对象。默认情况下,Git 会在一段时间后自动修剪不可达对象,但 --prune=now 会立即执行这个操作。

组合使用这两个选项,git gc --aggressive --prune=now 会执行一个更彻底、更即时的仓库清理和压缩。这可能会释放大量磁盘空间,特别是如果你有一个大的或旧的 Git 仓库,并且已经删除了很多文件或提交。

然而,需要注意的是,git gc 是一个重量级的操作,特别是在使用了 --aggressive 选项时。它可能会消耗大量的 CPU 和磁盘 I/O 资源,并且可能需要一些时间来完成。因此,最好在生产环境的非高峰时段运行这个命令,以避免影响其他正在进行的操作。

另外,如果你正在使用 Git 服务器(如 GitLab、GitHub Enterprise 等),它们可能已经有了自己的垃圾收集和清理机制。在这种情况下,你可能不需要手动运行 git gc 命令。始终最好先查阅你正在使用的 Git 服务器的文档,以了解它们如何处理仓库的清理和优化。

从版本库中移除一个文件,停止对该文件的版本控制

The git rm command will allows you to remove a file from git control.
The –cached option to git remove allows you to leave it on your hard
drive.
Every once in awhile a file gets checked into git that isn’t supposed to
be there. Common examples are configuration files, project files
generated by your IDE with personal settings and even the occasional
object file that someone decided to check in. These files are needed, so
often you can’t delete them entirely and the process of copying them
somewhere else, removing them from git and then replacing is painful,
not to mention prone to error.
By adding the –cached option to the git rm
command, you are able to remote the file file from git control while
keeping the file in your working tree. They command syntax is:

git rm --cached file

Git will no longer track this file even though it is still on your hard drive.
After running the above command, be sure to add an entry to your .gitignore file so that ‘file’ doesn’t show up in 'git status' and that it can’t accidentally be re-added later.

from : http://www.gitguys.com/how-to-remove-a-file-from-git-source-control-but-not-delete-it/


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 3400639399@qq.com

×

喜欢就点赞,疼爱就打赏

github