I have added a file named "file1.txt"
to git repo. After that I committed it, added a couple directories called dir1
and dir2
, and committed them to git repo.
Now the current repo has "file1.txt"
, dir1
and dir2
.
How can I delete "file1.txt"
without affecting others like dir1
and dir2
?
I tried a lot of the suggested options and none appeared to work (I won't list the various problems). What I ended up doing, which worked, was simple and intuitive (to me) was:
git rm
will only remove the file on this branch from now on, but it remains in history and git will remember it.The right way to do it is with
git filter-branch
, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.
If you want to completely obliterate it in one step, I recommend you to use
git forget-blob
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/
It is easy, just do
git forget-blob file1.txt
.This will remove every reference, do
git filter-branch
, and finally run the git garbage collectorgit gc
to completely get rid of this file in your repo.To delete a specific file
To clean all the untracked files from a directory recursively in single shot
Note: if you want to delete file only from git use below:
If you want to delete also from hard disk:
If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:
If you want to remove a folder inside another folder
Then, you can
commit
andpush
as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.Read more on official doc.
In my case I tried to remove file on github after few commits but save on computer
and later this file was ignored
More generally,
git help
will help with at least simple questions like this: