如何从存储库中删除“file1.txt”?


当前回答

2022年有效的新答案。

请勿使用:

git filter-branch

推送后,此命令可能不会更改远程回购。如果您在使用它之后进行克隆,您将看到没有任何变化,回购仍然具有较大的大小。此命令已过时。例如,如果使用中的步骤https://github.com/18F/C2/issues/439,这行不通。

你需要使用

git filter-repo

步骤:

(1) 在.git中查找最大的文件:

git rev-list --objects --all | grep -f <(git verify-pack -v  .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)

(2) Strat过滤这些大型文件:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force

or

 git filter-repo --path-glob '*.zip' --invert-paths --force

or

 git filter-repo --path-glob '*.a' --invert-paths --force

或无论您在步骤1中找到什么。

(3)

 git remote add origin git@github.com:.../...git

(4)

git push --all --force

git push --tags --force

完成!

其他回答

如果您有GitHub for Windows应用程序,您可以通过5个简单步骤删除文件:

单击同步。单击文件所在的目录,然后选择文件的最新版本。单击工具并选择“在此处打开外壳”在shell中,键入:“rm{filename}”并按enter键。提交更改并重新同步。

更一般地,git help至少可以帮助解决以下简单问题:

zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   :
   rm         Remove files from the working tree and from the index

2022年有效的新答案。

请勿使用:

git filter-branch

推送后,此命令可能不会更改远程回购。如果您在使用它之后进行克隆,您将看到没有任何变化,回购仍然具有较大的大小。此命令已过时。例如,如果使用中的步骤https://github.com/18F/C2/issues/439,这行不通。

你需要使用

git filter-repo

步骤:

(1) 在.git中查找最大的文件:

git rev-list --objects --all | grep -f <(git verify-pack -v  .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)

(2) Strat过滤这些大型文件:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force

or

 git filter-repo --path-glob '*.zip' --invert-paths --force

or

 git filter-repo --path-glob '*.a' --invert-paths --force

或无论您在步骤1中找到什么。

(3)

 git remote add origin git@github.com:.../...git

(4)

git push --all --force

git push --tags --force

完成!

gitrm从现在起只会删除该分支上的文件,但它将保留在历史中,git将记住它。

正确的方法是使用gitfilter分支,正如其他人在这里提到的那样。它将重写分支历史记录中的每个提交以删除该文件。

但是,即使在这样做之后,git也能记住它,因为在reflog、remotes、标记等中可以引用它。

如果您想在一个步骤中完全消除它,我建议您使用git forget blob

https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

这很简单,只需git忘记blob file1.text即可。

这将删除每个引用,执行gitfilter分支,最后运行git垃圾收集器gitgc,以彻底清除repo中的此文件。

如果需要从已确定的扩展名中删除文件(例如,已编译的文件),可以执行以下操作一次性删除所有文件:

git remove -f *.pyc