Delete accidentally submitted files from the GIT Repository

Source: Internet
Author: User
Tell git not to track a file any more (remove from repo) by Alvin Alexander on January 21,201 2

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
Git that I didn't mean to. These 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 wocould use this command:

git rm --cached bin/Foo.class

I shoshould 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 cocould 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.