我该如何从我的代码仓库中删除"file1.txt"
文件?
使用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 rm file.txt
可以将文件从版本库中删除,但也会从本地文件系统中删除。
要从版本库中删除文件,而不删除它们在本地文件系统中的副本,请使用:
git rm --cached file.txt
以下是我使用git来管理我的企业网站版本控制的确切情况,但“mickey”目录是一个tmp文件夹,用于与CAD开发人员共享私有内容。当他需要巨大的文件时,我创建了一个私人、未链接的目录,并通过浏览器ftpd文件供他提取。后来忘记了这一点,我从网站的基本目录执行了git add -A
。随后,git status
显示需要提交新文件。现在我需要从git的跟踪和版本控制中删除它们...
下面是我刚刚遇到的情况的示例输出,其中我无意中删除了.003
文件。谢天谢地,我不关心.003
的本地副本发生了什么,但其他一些当前更改的文件是我刚刚更新的网站,并且如果它们被删除了本地文件系统,那就太糟糕了!"本地文件系统"=实际的现场网站(不是很好的做法,但这是现实)。
[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: shop/mickey/mtt_flange_SCN.7z.001
# modified: shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.002
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# modified: shop/mickey/mtt_flange_SCN.7z.001
[~/www]$
更新:这个答案正在受到一些关注,所以我想提一下我的其他Git答案分享了一些很棒的资源:这个页面有一个图形可以帮助我解密Git。"Pro Git"书籍在线上对我也有很大帮助。
git commit -m "Just removing file1.txt"
的命令提交更改。如果您有一个远程代码库,那么您需要使用类似于 git push origin master
的命令将提交推送到该代码库中。 - ban-geoengineering首先,如果你正在使用git rm
,特别是对于多个文件,请注意任何通配符都将由shell解析,而不是git
命令。
git rm -- *.anExtension
git commit -m "remove multiple files"
如果你的文件已经在GitHub上,自2013年7月以来你可以直接从Web GUI中删除它!
只需查看代码库中任何文件,在顶部点击垃圾桶图标,然后提交删除操作,就像进行任何其他基于Web的编辑一样。
然后在本地仓库中执行“git pull
”,这也会将该文件从本地删除。
这使得这个答案成为了从git仓库中删除文件的(迂回)方法?
(更不用说GitHub上的文件已经在一个“git repo”中了)
(提交记录将反映删除该文件):
就这样,它消失了。
如需了解有关这些功能的帮助,请务必阅读我们的帮助文章:创建、移动、重命名和删除文件。
注意:由于它是一个版本控制系统,如果需要稍后恢复该文件,Git总会帮你的忙。
最后一句话意味着已删除的文件仍然是历史记录的一部分,您可以轻松地恢复它(但目前还不能通过GitHub Web界面实现):
请参见“在Git仓库中恢复已删除的文件”。
git-filter-repo
,如此处所示。然后使用git push --force
(因此请确保其他协作者知道您的新存储库历史记录)。 - VonC这是唯一对我奏效的选项。
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
注意:将*.sql替换为您的文件名或文件类型。请非常小心,因为这将遍历每个提交并删除此文件类型。
编辑: 注意 - 在运行此命令后,您将无法推送或拉取 - 您将看到'不相关历史记录'的拒绝消息,您可以使用'git push --force -u origin master'来推送或拉取。
git push --force -u origin master
推送上去。 - AlbertMarkovski此外,如果要删除一个文件夹及其所有子文件夹或文件,请使用:
git rm -r foldername
更一般地说,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 help
很令人困惑,但对于简单的事情来说还是不错的 :) - James Polleyrm
命令会将文件“标记为删除”,而不是“从索引中删除”(尽管对于新手来说这仍然没有意义)。 - Roman StarkovGreg Hewgill的回答,被Johannchopin编辑过,对我很有帮助,因为我并不关心完全从历史记录中删除文件。 在我的情况下,它是一个目录,所以我做的唯一更改就是使用:
git rm -r --cached myDirectoryName
使用"git rm --cached file1.txt" 的替代方法是:
git commit -m "deleted myDirectoryName from git"
git push origin branch_name
bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"
如果你希望从代码库和文件系统中删除文件,则有两个选项:
如果文件没有在索引中被暂存更改:
bykov@gitserver:~/temp> git rm file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt"
如果文件在索引中有已暂存的更改: bykov@gitserver:~/temp> git rm -f file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt"
git rm
命令只会从当前分支移除文件,但文件依然存在于历史记录中,Git 会记住它。
正确的做法是使用 git filter-branch
,如其他回答所述。这将重写该分支历史上的每个提交以删除该文件。
但即使这样做了,Git 仍可记忆该文件,因为在 reflog、远程、标签等处可能仍有对它的引用。
如果您想一次性完全删除它,我建议您使用 git forget-blob
。
很容易,只需执行 git forget-blob file1.txt
。
这将删除所有引用、执行 git filter-branch
,最后运行 Git 垃圾回收器 git gc
以彻底清除您 repo 中的此文件。
git mv
重命名了该文件,并清空了所有内容后提交了它。 - Harsha N Hegdegit rm --cached file1.txt
如果你想从硬盘中删除:
git rm file1.txt
如果您想删除一个包含少量文件的文件夹,您需要使用递归命令来删除,如下所示:
git rm -r foldername
git rm -r parentFolder/childFolder
然后,您可以像往常一样提交(commit)
和推送(push)
。但是,如果您想恢复已删除的文件夹,您可以按照以下步骤操作:从Git中恢复已删除的文件是可能的。
From doc:
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…
OPTIONS
<file>…
Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you
may need to shell-escape them. A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option to be explicitly given.
-f
--force
Override the up-to-date check.
-n
--dry-run
Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
-r
Allow recursive removal when a leading directory name is given.
--
This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for
command-line options).
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
--ignore-unmatch
Exit with a zero status even if no files matched.
-q
--quiet
git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
git rm
是正确的答案,但请记住该文件仍然会留在历史记录中。 如果您希望删除一个包含敏感信息的文件,则需要采取更严厉的措施。(改变历史记录,尤其是对于您已经推送的内容,是一项激烈的行动,如果可能的话应该避免。) - Keith Thompson