如何从 Git 存储库中删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2047465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete a file from a Git repository?
提问by webminal.org
I have added a file named "file1.txt"
to a Git repository. After that, I committed it, added a couple of directories called dir1
and dir2
, and committed them to the Git repository.
我添加了一个名为"file1.txt"
Git 存储库的文件。之后,我提交了它,添加了几个名为dir1
and的目录dir2
,并将它们提交到 Git 存储库。
Now the current repository has "file1.txt"
, dir1
, and dir2
. How can I delete "file1.txt"
without affecting others, like dir1
and dir2
?
现在目前的版本库"file1.txt"
,dir1
和dir2
。如何在"file1.txt"
不影响其他人的情况下删除,例如dir1
和dir2
?
回答by Greg Hewgill
Use git rm
.
使用git rm
.
If you want to remove the file from the Git repository and the filesystem, use:
如果要从 Git 存储库和文件系统中删除文件,请使用:
git rm file1.txt
git commit -m "remove file1.txt"
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
但是,如果您只想从 Git 存储库中删除文件而不是从文件系统中删除它,请使用:
git rm --cached file1.txt
git commit -m "remove file1.txt"
And to push changes to remote repo
并将更改推送到远程仓库
git push origin branch_name
回答by Chris K
git rm file.txt
removes the file from the repo but also deletes it from the local file system.
git rm file.txt
从 repo 中删除文件,但也从本地文件系统中删除它。
To remove the file from the repo and notdelete it from the local file system use:git rm --cached file.txt
要从 repo 中删除文件而不是从本地文件系统中删除它,请使用:git rm --cached file.txt
The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a git add -A
from the website's base directory. Subsequently, git status
showed the new files needing committing. Now I needed to delete them from git's tracking and version control...
下面的确切情况是我使用 git 来维护我公司网站的版本控制,但“mickey”目录是一个 tmp 文件夹,用于与 CAD 开发人员共享私人内容。当他需要巨大的文件时,我创建了一个私有的、未链接的目录并将其中的文件 ftpd 供他通过浏览器获取。忘记我这样做了,我后来git add -A
从网站的基本目录中执行了一个。随后,git status
显示需要提交的新文件。现在我需要从 git 的跟踪和版本控制中删除它们......
Sample output below is from what just happened to me, where I unintentionally deleted the .003
file. Thankfully, I don't care what happened to the local copy to .003
, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).
下面的示例输出来自我刚刚发生的事情,我无意中删除了.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]$
Update:This answer is getting some traffic, so I thought I'd mention my other Git answershares a couple of great resources: This pagehas a graphic that help demystify Git for me. The "Pro Git" book is onlineand helps me a lot.
更新:这个答案获得了一些流量,所以我想我会提到我的另一个 Git 答案分享了一些很棒的资源:这个页面有一个图形可以帮助我揭开 Git 的神秘面纱。在“临Git的”一书是在网上,并帮助了我很多。
回答by VonC
First, if you are using git rm
, especially for multiple files, consider any wildcard will be resolved by the shell, not by the git
command.
首先,如果您正在使用git rm
,尤其是对于多个文件,请考虑任何通配符将由 shell 解析,而不是由git
命令解析。
git rm -- *.anExtension
git commit -m "remove multiple files"
But, if your file is already on GitHub, you can (since July 2013)directly delete it from the web GUI!
但是,如果您的文件已经在 GitHub 上,您可以(自 2013 年 7 月起)直接从 Web GUI 中删除它!
Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.
只需查看存储库中的任何文件,单击顶部的垃圾桶图标,然后像任何其他基于 Web 的编辑一样提交删除操作。
Then "git pull
" on your local repo, and that will delete the file locally too.
Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub isin a "git repo")
然后git pull
在您的本地存储库中使用“ ”,这也会在本地删除该文件。
这使这个答案成为从 git repo 中删除文件的(迂回)方式?
(且不说在GitHub上一个文件是在“混帐回购协议”)
(the commit will reflect the deletion of that file):
(提交将反映该文件的删除):
And just like that, it's gone.
For help with these features, be sure to read our help articles on creating, moving, renaming, and deletingfiles.
Note: Since it's a version control system, Git always has your back if you need to recover the file later.
就这样,它消失了。
如需这些功能的帮助,请务必阅读我们关于创建、移动、重命名和删除文件的帮助文章。
注意:由于它是一个版本控制系统,因此如果您稍后需要恢复文件,Git 始终会支持您。
The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):
最后一句意味着被删除的文件仍然是历史的一部分,你可以很容易地恢复它(但还不能通过 GitHub 网络界面):
See "Restore a deleted file in a Git repo".
请参阅“在 Git 存储库中恢复已删除的文件”。
回答by Jason Glisson
This is the only option that worked for me.
这是唯一对我有用的选择。
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.
注意:将 *.sql 替换为您的文件名或文件类型。要非常小心,因为这将通过每次提交并删除此文件类型。
EDIT: pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull
编辑:注意 - 在此命令之后,您将无法推或拉 - 您将看到“无关历史”的拒绝,您可以使用“git push --force -u origin master”来推或拉
回答by Raja
Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:
此外,如果它是要删除的文件夹及其后续子文件夹或文件,请使用:
git rm -r foldername
回答by James Polley
More generally, git help
will help with at least simple questions like this:
更一般地说,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
回答by James Polley
If you want to delete the file from the repo, but leave it in the the file system (will be untracked):
如果您想从 repo 中删除文件,但将其保留在文件系统中(将不被跟踪):
bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"
If you want to delete the file from the repo and from the file system then there are two options:
如果要从 repo 和文件系统中删除文件,则有两个选项:
If the file has no changes staged in the index:
bykov@gitserver:~/temp> git rm file1.txt bykov@gitserver:~/temp> git commit -m "remove file1.txt"
If the file has changes staged in the index:
bykov@gitserver:~/temp> git rm -f file1.txt bykov@gitserver:~/temp> git commit -m "remove file1.txt"
如果文件在索引中没有暂存更改:
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"
回答by nachoparker
git rm
will only remove the file on this branch from now on, but it remains in history and git will remember it.
git rm
从现在开始只会删除此分支上的文件,但它会保留在历史记录中并且 git 会记住它。
The right way to do it is with git filter-branch
, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.
正确的方法是使用git filter-branch
,正如其他人在此处提到的那样。它将重写分支历史记录中的每个提交以删除该文件。
But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.
但是,即使这样做了,git 仍然可以记住它,因为在 reflog、remotes、tags 等中可以引用它。
If you want to completely obliterate it in one step, I recommend you to use git forget-blob
如果你想一步完全抹掉它,我建议你使用 git forget-blob
It is easy, just do git forget-blob file1.txt
.
很简单,做就行git forget-blob file1.txt
。
This will remove every reference, do git filter-branch
, and finally run the git garbage collector git gc
to completely get rid of this file in your repo.
这将删除每个引用, do git filter-branch
,最后运行 git 垃圾收集器git gc
以完全摆脱你的 repo 中的这个文件。
回答by Blasanka
Note: if you want to delete file only from git use below:
注意:如果您只想从 git 中删除文件,请使用以下命令:
git rm --cached file1.txt
If you want to delete also from hard disk:
如果您还想从硬盘中删除:
git rm file1.txt
If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:
如果要删除文件夹(该文件夹可能包含几个文件),则应使用递归命令删除,如下所示:
git rm -r foldername
If you want to remove a folder inside another folder
如果要删除另一个文件夹中的文件夹
git rm -r parentFolder/childFolder
Then, you can commit
and push
as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.
然后,你可以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 [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…?
选项
<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
可能需要对它们进行 shell-escape。可以给出前导目录名称(例如,用于删除 dir/file1 和 dir/file2 的 dir)以删除目录中的所有文件,并递归地删除所有子目录,但这需要明确给出 -r 选项。
-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
命令行选项)。
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
--忽略不匹配
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.
回答by Suhaib
Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.
如果您想使用 rm 命令从本地文件夹中删除文件,然后将更改推送到远程服务器,则另一种方法。
rm file1.txt
git commit -a -m "Deleting files"
git push origin master