Search code examples
gitgithubgit-push

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?


Solution

  • 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 :)