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


当前回答

这是我唯一的选择。

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'

注意:用文件名或文件类型替换*.sql。要非常小心,因为这会经过每次提交并删除此文件类型。

编辑:注意-在这个命令之后,您将无法推或拉-您将看到拒绝“不相关的历史记录”,您可以使用“git push--force-u origin master”来推或拉

其他回答

使用git rm。

如果要从Git存储库和文件系统中删除文件,请使用:

git rm file1.txt
git commit -m "remove file1.txt"

但如果您只想从Git存储库中删除文件,而不想从文件系统中删除,请使用:

git rm --cached file1.txt
git commit -m "remove file1.txt"

并将更改推送到远程回购

git push origin branch_name

更一般地,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

若你们并没有在本地回购中归档,而是在git回购中归档的话,那个么只需通过web界面在git repo中打开文件,然后在界面的右角找到“删除”按钮。单击此处,查看界面删除选项

删除特定文件

git-rm文件名

在一次快照中递归清理目录中所有未跟踪的文件

git-clean-fdx

在我的案例中,我尝试在几次提交后删除github上的文件,但保存在计算机上

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master

后来这个文件被忽略了