开发者

Remove references of physically removed files from remote Git repository

I have deleted a lot of files from my Git repository (from many开发者_JS百科 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?


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


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're talking about removing files and committing this removal then you should use

git rm filename

This command will remove the file itself and stage the removal for commit right away. So, in this case you will need only to execute

git commit -m "Remove files" #everything staged will be committed

BTW same is true when you wan to move or rename files. Don't do that using shell move command, but rather using "git mv [source] [destination]"

Now if you have removed them without using "git rm"

you should stage them first:

git add -u
git commit -m "Remove files"

Now if you want them to disappear from the entire history, so there will be no mentioning of those files whatsoever, then you have to use

git filter-branch 

Git filter-branch documentation

Please, note that most of the time you actually don't need to use filter-branch command as it is pretty advanced stuff. The best sample use case for it is if you have committed the file with some critical info like your credit card number and expiration date and pushed this file to public repo. In this case, changing this value to some irrelevant value or deleting file is not enough - you have to use filter branch;) ...And pray that nobody was quick enough to fetch from your repo;) ...And then report your credit card as stolen anyway...;)


They should show up in your index as deleted. Just stage them and they will be deleted.

Use git add -i to make the staging process quicker.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜