Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove references of physically removed files from remote Git repository

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?

like image 685
Rana Avatar asked Mar 28 '11 11:03

Rana


People also ask

How do I remove files from a remote git repository?

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.

How do I remove a file from a git reference?

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.


2 Answers

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:

  • Remove all deleted files from "changed but not updated" in Git

In future, it's best to delete files with git rm in the first place :)

like image 97
Mark Longair Avatar answered Sep 24 '22 20:09

Mark Longair


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.

like image 32
Bombe Avatar answered Sep 22 '22 20:09

Bombe