Suppose you have been working for a long time on a project, and now you want to delete some files from you project because … (the reason does not really matter at this point).

You may want to completely erase a file or a whole directory from your repository. The file or directory may be kept in the local copy or deleted as well.

If you want a complete removal use git rm:

Complete removal
1
2
3
4
$ git rm file_to_delete.rb
$ # file_to_delete.rb removed from both your working copy and repository tree.
$ git rm -r directory_to_delete
$ # directory_to removed from both your working copy and repository tree.

If you want to keep your local copy use git rm —cached.

Retaining local copy
1
2
3
4
$ git rm --cached file_to_delete.rb
$ # file_to_delete.rb removed only from repository tree.
$ git rm --cached -r directory_to_delete
$ # directory_to removed only from your repository tree.

In the latter case don’t forget to add the file or directory to your .gitignore file, otherwise Git will bother you with annoying status messages.

One last thing you should keep in mind is that erasing data from a repository means only that the information is not present in the actual commit and in all future incarnations of the code. The data can be restored from the previous states of your repository. If you want to wipe your sensitive data consider reading the appropriate GitHub Help page on this topic.

Comments