3

I've been working with my repo on bitbucket for awhile and there's a few files that I work with locally. I've removed some files (just used rm from linux), committed with -am "message", and pushed to master.

Now that I go to my repo, I see some files that exist on the repo but not locally.

How can I tell my git repository to delete any files that don't exist locally?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
LewlSauce
  • 5,326
  • 8
  • 44
  • 91
  • Typically, you would remove files locally, then push to the remote repository, and not the other way around. Why do you want to delete these files from the remote, and have you ascertained that they are not of any importance? – Tim Biegeleisen Jan 18 '16 at 04:01
  • Yeah they're not important. I just forgot to use git rm when removing them locally. – LewlSauce Jan 18 '16 at 04:05
  • If you _forgot_ to delete them locally, then they should appear locally _and_ on the remote. Am I missing something here? – Tim Biegeleisen Jan 18 '16 at 04:06
  • Yes. I forgot to specifically use "git rm" when removing them locally. I used "rm" to remove them locally, which apparently has no affect on the repository. – LewlSauce Jan 18 '16 at 04:07
  • When you do a `git push` they should be deleted from the HEAD of the remote branch as well. Just `git rm` the files you forgot, push, and be done with it. – Tim Biegeleisen Jan 18 '16 at 04:07
  • I can't use "git rm" on a file that doesn't exist. I get an error stating that "fatal: pathspec 'test.txt' did not match any files" and I'm doing it from the same folder that it used to be in (and is currently in on the repo) – LewlSauce Jan 18 '16 at 04:09
  • No. If the file is still on the repo it might be because someone else resurrected it there. You should `git pull` to sync up, then delete again. Do not think of directly hacking the remote repository; this is not how Git works. – Tim Biegeleisen Jan 18 '16 at 04:10
  • When I git pull, everything is already up-to-date. – LewlSauce Jan 18 '16 at 04:11
  • If the `git pull` shows you as being up to date, then the file does _not_ exist at the HEAD of your remote branch, i.e. you really did delete it. As the partial answer mentions, perhaps you are seeing the file in the history somewhere and getting spooked? This is how Git works. The file is not there at the HEAD, but does exist in earlier commits. – Tim Biegeleisen Jan 18 '16 at 04:13
  • Hmm ok. I just browsed to it in the repo and happened to notice it. Not sure what I'm missing but I'll continue to do my research. Thanks for your help. – LewlSauce Jan 18 '16 at 04:14

4 Answers4

2

Now that I go to my repo, I see some files that exist on the repo but not locally.

The files are stored in your git repository but they are not part of your recent code (workdir).

If you want to totally remove them from git (from history) you will have to clean the repository and delete them.

To do it you can use filter-branch or to use this tool.

enter image description here


How can I tell my git repository to delete any files that don't exist locally?

Once git track files which you wish to remove you will have to first remove them from the cache (index).

git rm --cached

Which files to remove? you will need to have a list of the files you wish to remove. This can be done either manually if you know your files or generate them with script.


How to get list of files to be removed?

There are few ways. (format-patch, log and more)

Here is a sample on how to view the files which were committed (git > 2.5)

git log --cc --stat

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
1

If you removed the files with rm, they should appear in git status under Changes not staged for commit: as deleted. If that's correct, you can do either git rm <file> or git add <file>, and then commit. Don't worry, the effect for both is the same in your case. This will remove the files from the index of your working directory.

When you rm a file, git knows that it's missing from your current working directory, but it does not stop tracking the file. You must add this change to the cache (or staging area) and then commit the changes. git rm has the same effect as rm the file and then git add the file. If the file exists in the working directory, git rm also deletes it, otherwise it does not fail.

Note that this does not remove the files from the repository, so if the files are sensitive (e.g. private keys or passwords) you should remove the file from all objects as described here.

Community
  • 1
  • 1
micromoses
  • 6,747
  • 2
  • 20
  • 29
  • Had the exact same need as the question and this was a great solution even in 23. If you use terminal to go to the place where the file shows in the repo but not locally, it actually shows, so once I git rm that, commited and pushed the changes, worked perfectly. – Blu3 Apr 26 '23 at 10:18
0

That files remain in earlier revisions forever, but shouldn't be in the last revision. You can only recreate you repository.

Dmitriy Korobkov
  • 867
  • 1
  • 11
  • 25
0

commited with -am "message"

Even if you haven't done a git rm, the commit -a is enough to pick up those deleted files:

git commit man page:

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.

That means the git push has pushed those deletion. Try and clone again your repo elsewhere, to see that those files are no longer visible in HEAD.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250