I have deleted a lot of files from my Git repository (from many different directories) physically.
I can still see their reference and if I make a commit to GitHub, old files are still there.
I need to remove those references from the remote repo server (GitHub) and as well as from my local PC.
How can I remove these references from the repositories?
The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.
To remove the remaining references to the old history in the local repository, “git for-each-ref” will print all refs matching the “refs/original” in the repository with the “delete” command prefixed. This command is piped to the “git update-ref” command which will delete any reference to the old history.
You need to create a "commit" (git's terminology for a new version) which has those files deleted, and then push that to GitHub. I guess if you type git status
, you'll see something like this:
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: old-file.txt
# deleted: another-old-file.txt
# deleted: unneeded.c
If there are no other changes listed in the output of git status
, it would be safe to do:
git add -u
git commit -m "Delete many old files"
... and then push to GitHub. (The git add -u
says to stage for the next commit any changes to files that are being tracked by git, which would include these files which you've deleted by hand locally.)
However, if there are other changes listed in that output, it would be better to try something more precise, such as suggested here:
In future, it's best to delete files with git rm
in the first place :)
If you want to remove a file from all commits of the repository, you need git filter-branch
. Examples for using it can be found all over the internet, e.g. on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With