从git仓库中删除误提交的文件
2013-05-10 16:19
267 查看
Tell Git not to track a file any more (remove from repo)
By Alvin Alexander on January 21, 2012Git rm FAQ: How do I tell Git not to track a file (or files) any more? That is, I want to remove the file from the Git repo?
While working on an application named "sarah" yesterday (named for the house known as "SARAH" in the tv series Eureka), I accidentally checked some files into
Git that I didn't mean to. These were were primarily binary files in my project's "bin" and "target" directories.
Because I didn't want these files in my Git repository, I needed a way to remove them from the repository, but still keep them on disk. Fortunately there's a Git command for just this situation:
git rm --cached [filenames]
For example, here's how I removed all the files I wanted to delete from one of my "bin" subdirectories:
git rm --cached bin/com/devdaily/sarah/\*
I use the unusual "\*" syntax at the end of that command instead of "*" because you need to escape the "*" from the git command. In a simpler example, if there was just one file in the bin directory named Foo.class that I wanted to remove from the Git repository,
I would use this command:
git rm --cached bin/Foo.class
I should be clear that what this command means is:
You want to keep these files on your hard drive, but you don't want Git to track them any more.
(I got this quote directly from page 25 of the excellent book, Pro Git.)
More Git remove (don't track) examples
If it helps to see some more examples, here's a list of all the "git rm --cached" commands I ended up running:git rm --cached src/main/resources/\* git rm --cached target/scala-2.9.1/classes/\* git rm --cached target/streams/\$global/compilers/\$global/out
After running a few more git rm commands, I learned that I could have just run the following git command to remove all files in the target directory from the git repository:
git rm --cached target/\*
Followup: I just read that this command may still keep the files in the history, and completely removing them is a more difficult problem. However, I'm not very concerned about that, in my case I just want Git to stop tracking those files, so this solution
works fine for me.
相关文章推荐
- 如何永久删除git仓库中敏感文件的提交记录
- 本地用git提交和删除github上远程仓库的文件
- 如何永久删除git仓库中敏感文件的提交记录
- Git 仓库中删除大文件的历史提交记录
- git正确的删除远程仓库的文件并用.gitignore忽略提交此文件
- git正确的删除远程仓库的文件并用.gitignore忽略提交此文件
- Git 删除远程仓库文件,并忽略提交文件
- git正确的删除远程仓库的文件并用.gitignore忽略提交此文件
- git 删除远程仓库的某次提交
- git 删除仓库的文件
- git删除远程仓库的文件或目录
- git——创建仓库、提交、忽略文件、撤消更改、查看提交记录
- git 在仓库中添加提交文件
- 初次使用 git 的“核弹级选项”:filter-branch 从仓库中删除文件
- Git恢复被修改的文件--未提交到远程仓库
- Git简明教程--4.文件添加和版本提交到本地仓库
- git 创建仓库,提交文件
- GIT 如何合并另一个远程Git仓库的文件到本地仓库里某个指定子文件夹并不丢失远程提交记录
- git删除远程仓库的文件或目录
- git 删除远程仓库中的文件或者文件夹