3

I have a git project. Some source files have been dispensed with in the latest version. I want to remove them from the repo from now forward, but preserve their history thus far. I.e. If I check out yesterday's repo it had better have these files or it wouldn't compile.

I know it's probably "git rm", but I have 2 problems with git rm right now. One is that I can't find any documentation that definitively says "git rm preserves the file's history in the repo". And I've already deleted these files from my working directory, so I doubt I can git rm files that aren't even there. (I could copy them back, just to be able to do a git rm, but I want to make sure I'm doing things the right.)

Joe C
  • 2,728
  • 4
  • 30
  • 38
  • 1
    It's git rm --cached And it preserves the file's history in the repo. – Shawn Balestracci Aug 16 '13 at 02:51
  • 1
    It's actually pretty hard to delete a file's history in git, which is probably a good thing. – beatgammit Aug 16 '13 at 02:55
  • Next time, you can directly use `git rm` from the beginning instead of using `rm` first. `git rm` will remove the file from the working directory and will tag it for removal in the next commit in Git's index. – Vincent Cantin Aug 16 '13 at 03:12
  • The main difficulty is persuading git to the reverse and delete it from all commits... For example when you accidentally commited a file with all your passwords in. – jcoder Aug 16 '13 at 15:00

2 Answers2

7

It is indeed git rm.

Of course it preserves the files history. It is in the prior commits and so you can rest, assured that it is there.

And yes, you can git rm files that you did just delete, the only difficulty is likely that you can't tab complete the file names, as they don't exist.

When working in a git repository it easier if you can remember to do git rm <file> instead of doing the rm <file> and then git rm <file>.

As git, by its very purpose, "preserves the files history in the repository" it is more likely to see a comment when this behavior isn't happening. A warning note that says the opposite of the design, such as "Warning, this function does not preserve history in the repository", is more likely than stating that an option of git does preserve file history.

vgoff
  • 10,980
  • 3
  • 38
  • 56
  • If the behavior OP desires is the default, how do you perform the action he thought was the default? In other words, how do I nuke a file and its history? – idoby Aug 16 '13 at 14:46
  • 1
    https://help.github.com/articles/remove-sensitive-data is what you are looking for in that case. From SO as well, this is a good [answer](http://stackoverflow.com/a/872700/485864) to that question as well. And git-scm.org addresses this as well in their documentation. – vgoff Aug 16 '13 at 14:48
1

git rm removes a file, and the subsequent commit commits the removal of that file. Removals are a change like any other, and are tracked by git. Checking out an old repo will restore the whole repo as it originally was, including files that are no longer in the tip of the tree.

nneonneo
  • 171,345
  • 36
  • 312
  • 383