tags:

views:

228

answers:

1

Previously, the following was my .gitignore file.

...
...
config/database.yml
.DS_Store

Later i created a app_config.yml file in the config directory and comitted it.

Now, I realized that I don't need that app_config.yml file in the git repository. And I modified my .gitignore file

...
config/app_config.yml
config/database.yml
.DS_Store

Now, when i commit, that app_config.yml file is still in my repo. I want to remove that file from my repo. How can I do it??

+9  A: 

As mentionned in "ignoring doesn't remove a file "
(the following quote the great article from Nick Quaranto,
in his blog git ready "learn git one commit at a time"):

When you tell Git to ignore files, it’s going to only stop watching changes for that file, and nothing else.
This means that the history will still remember the file and have it
.

If you want to remove a file from the repository, but keep it in your working directory, simply use:

git rm --cached <file>

However, this will still keep the file in history.

If you actually want to remove it from history, you really have two options:

  • rewrite your repository’s commits, or
  • start over.

Both options really suck, and that’s for a good reason: Git tries hard not to lose your data.
Just like with rebasing, Git forces you to think about these kinds of options since they are destructive operations.

If you did actually want to remove a file from history, git filter-branch is the hacksaw you’re looking for.
Definitely read up on its manpage before using it, since it will literally rewrite your project’s commits. This actually is a great tool for some actions, and can do all sorts of stuff like totally removing an author’s commits to moving the project root folder around. The command to remove a file from all revisions is:

git filter-branch --index-filter 'git rm --cached <file>' HEAD

This action can definitely be useful when you need to blow out sensitive or confidential information that may have been placed in your repository

VonC