從git倉庫中刪除誤提交的檔案

來源:互聯網
上載者:User
Tell Git not to track a file any more (remove from repo)By Alvin Alexander on January 21, 2012

Git 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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.