tags:

views:

72

answers:

2

After checking out a branch in a repository git always prints a list of roughly 25 files that were deleted long ago. I always just ignored that output but recently I had to do a hard reset, which I assumed would just go back to the last commit, which it did but it also restored those 25 files. Is there some way of removing those files from git? I tried git rm but since they were long deleted that didn't work!

+1  A: 

If they were deleted Git wouldn’t restore them upon a git reset --hard so git rm should indeed be the correct way to get rid of them. (Don’t forget to commit afterwards!)

You might want to take a look at what Git thinks has happened with these files. git log path/to/file might help you there.

Other things to check are:

  • are you on the correct branch? gitk --all will give you a nice overview of all your branches, including the ability to search for changes to a specific file only.
  • are you on the correct repository? If you have multiple checkouts of your repository all over your harddriver you might simply be looking at an old one.
  • maybe someone has added these files again? Again, check the commit logs using git log.
Bombe
When I type git log:master $ git log app/views/participants/index.html.erbfatal: ambiguous argument 'app/views/participants/index.html.erb': unknown revision or path not in the working tree.Yet that is one of the files that keeps on appearing with a 'D' next to it when I checkout. I believe that those files were deleted normally, without the help of git, could that be the problem?
LDK
+1  A: 

It sounds like you deleted the files from your directory, but never committed the deletions (like Bombe said, with git rm). That's why they came back after the git reset.

Deleting a file is just like any other change, it has to be recorded in the repository (which is what git rm [file] will do).

Mike Tierney
Just to be clear. After I've made some changes I usually do the following: "git add .", then "git commit -m "change made"". If I deleted a file should I then type: "git rm .", then "git commit -m "removed file"". Or will "git rm ." just remove everything (even files that were not deleted?).
LDK
Yeah, I made that mistake - "git rm ." will removing everything. My current workflow is to use `git commit -a -m 'Commit message'`, which will stage *all* of the changes you've made, with the exception of untracked files (which you'll still need to do `git add <path>` on).
Mike Tierney
so the -a option gets rid of the need to use "git add ." and "git rm ."?
LDK
Correct, but only with regard to tracked files. From the Manual (Git-Commit):"OPTIONS: -a, -all: Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected."
Mike Tierney