Git remove file permanently

Oh!!! I have added a file that I didn’t want!! What is the next? How can I remove it from my repository?
Don’t worry! You can remove a file from your repository since you are using Git.

If we don’t know when we add that file we can remove it from the beginning using the following command:

If we want to remove it from a concrete commit (usually the last). We should add the commit.

We also can remove it from a range of commits as you can see in the following example:

Ok, I have just finished to remove the file from my repository, what is next?
You should refresh the reflog removing the dereference file and force garbage collect:

Unless you are making a new repository, you should update all branches and tags to remote. And all your team must remove and clone the repository to avoid including again the removed file.

I hope this article was useful. We can always add unwanted files when we add files using the command “$ git add .”, for this reason you should avoid it and always add your specific files one by one.

Leave a Reply