Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removed file without git remove. Now it's in repo, and not local. How to remove from repo?

Tags:

git

github

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?

like image 256
LewlSauce Avatar asked Dec 03 '25 14:12

LewlSauce


2 Answers

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

like image 171
CodeWizard Avatar answered Dec 06 '25 05:12

CodeWizard


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.

like image 32
micromoses Avatar answered Dec 06 '25 05:12

micromoses



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!