0

A couple of days ago, I noticed that somehow had a committed file that was also ignored in .gitignore. It's the site's main config file, and my development copy had somehow gotten committed.

In order to remove the file, I created a commit that removed the line from .gitignore and deleted the file in question. Then I created another commit that added the .gitignore line back in.

But now, whenever those commits are applied to someone's environment, their ignored config file is deleted! How can I fix my repo so that this doesn't happen anymore?

mattalxndr
  • 9,143
  • 8
  • 56
  • 87
  • 1
    Given that the file was i your repository already I would check: http://stackoverflow.com/questions/625919/how-to-ignore-a-file-which-is-already-comitted-in-the-previous-commit?rq=1 – Michael Durrant Jan 27 '13 at 16:15

1 Answers1

0

You can’t really. If your current HEAD commit tracks the file and as such includes it, and a subsequent commit doesn’t, Git will apply this difference by removing the file, because that is what happened in the history. If you want to save the file you can either temporarily rename it before checking out the new version to prevent Git from removing it, or you can check out an older version of it using git checkout oldcommit -- config.file.

The role of the .gitignore file is not to prevent anything from being added or removed in your repository. It just affects the mechanisms of Git to detect untracked files, e.g. by hiding ignored (but untracked!) files from the output of git status. It will not prevent you from explicitely adding—or removing—files from the repository though.

That being said, you did not have to remove the line from .gitignore to remove the file from the repository. You could have just used git rm --cached config.file to untrack it; without phyiscally deleting the file and without touching the gitignore.

poke
  • 369,085
  • 72
  • 557
  • 602