How to Delete Unwanted Files from Remote Git Repo While Keeping Them on Local Repo

Delete files from GitHub while keeping them on your local repository (eg: .env, node_modules, etc)

Oh no! Did I just do that?

That's exactly how I reacted after realizing that I just pushed the node_modules to my GitHub account. Not only was that time-wasting, but it was also very unnecessary.

Now, I need to figure out how to remove the node_modules from the remote repository while maintaining them on my local repository.

This is because even though it isn't important to have them on platforms like GitHub where the remote repositories are hosted, you need them for testing purposes on your local repository.

I just found myself making the same mistake again a couple of days after I made it for the first time. I believe that I may not be alone, and therefore a guide on how to solve the problem is necessary. That is my motivation for writing this article.

Why does this problem happen?

When working with a remote Git repository especially one which is hosted publicly like GitHub, there are some files that you may not want to push there.

You can achieve this by setting up a .gitignore file and listing all files and directories that should be ignored anytime that you git push your commits.

Hence, if you do not have a .gitignore file set up or you forget to add the specific file or directory to your .gitignore file then you will end up pushing them to GitHub just as I mistakenly did.

Inefficient way to solve this problem - that I initially thought about

I will introduce you to an efficient way to solve the problem, but let me first share with you an inefficient way that I thought about before finding the right way to do it.

  • Delete the file or directory directly from the remote repository. That is to say, go to the Git hosting platform (eg: GitHub) and delete those unwanted files.

  • Pull the updates on your local machine and commit the changes. After committing the changes go ahead and push the commit back to Github.

Why is this solution not effective?

After deleting from the remote and pulling the updates, the file/directory will also be automatically deleted in your local repository. You would therefore have to find another means to get them back or recreate them.

Now, let's take a look at the most efficient way to solve this problem.

How to solve this problem

The solution to the problem is quite simple, though. In just three simple steps you can delete the unwanted files from your remote repository while keeping them on your local repository.

Below are the steps to follow:

  1. Remove from cache

    You would have to take note of all the files and directories that you do not want on your remote repository. In my case, it was the node_modules directory.

    One other common file that you may find yourself pushing when you are not supposed to showcase publicly is the .env file.

    You can run the git rm command to remove this file/directory. But remember that we do not want to remove it from our local repository. We only want to remove it from the remote repository.

    To do that, you should just remove it from the cache.

    Also, because it could be multiple files or a directory, you would have to delete recursively (with the -r flag).

The right command to use therefore is git rm -r --cached followed by the name of the directory or files that you want to remove.

git rm -r --cached node_modules
  1. Set up a .gitignore file

    Now that you have removed the files from the cache, go ahead and create your .gitignore if you don’t have it yet. Then add the file, list of files or directory that you want to avoid pushing to the remote repository.

    An example is shown below

     node_modules
     .env
     .env.*
    
  2. Commit the changes

    After removing it from the cache and adding to your gitignore, you can proceed to commit the changes by running a git commit command with the right commit message (using the -m flag).

git commit -m "Remove 'node_modules' directory from remote repo"

Push the changes

Once the changes have been committed, you have to proceed to push the changes to the remote repository. That is when the file or directory will be removed from the remote repository.

git push

Conclusion

I hope you found this helpful. I share various helpful software engineering tips like this especially ones that I find challenging myself and eventually figure out. You may want to subscribe to this blog to receive any new articles that I publish here.

Also, I share a lot of motivation for software engineering as well as document my software engineering journey on Twitter. You can check me out on Twitter or better still, send me a DM, and let's chat. I can't wait to connect with you.

Finally, you may also want to check out my YouTube channel where I publish various programming tutorials. Thanks for reading and catch you in the next article.

Did you find this article valuable?

Support Dr. Ehoneah Obed by becoming a sponsor. Any amount is appreciated!