Remove unwanted files from GitHub

Remove unwanted files from GitHub
pexels-realtoughcandy-11035539

When we’re working on GitHub projects, it’s pretty common to accidentally add unnecessary files or folders to our repository. Things like local configuration files, temporary logs, or dependency folders often sneak in.

Contrary to what you might think, simply adding these files to your .gitignore after pushing them to GitHub isn’t enough to remove them from the repository.

Below, I’ll explain how to get this done.

Accidentally uploaded files

Let’s say you accidentally uploaded a folder called /oldtest to GitHub. This folder might contain temporary files or settings specific to your local development environment. It’s usually unnecessary for the project’s main repository and can just clutter things up.

Bellow you can find steps you’ll need to follow to clean up those unwanted contents from your GitHub repository:

1. Update your .gitignore file

First and most importantly, we need to tell Git to ignore these types of files or folders in the future. Open your .gitignore file, which you’ll find in your project’s root directory, with a text editor. If you don’t have one, just create it. Then, add the name of the folder you want Git to ignore. For instance, to ignore the /oldtest folder:

/oldtest

This line tells Git to completely ignore the /oldtest folder and everything inside it.

After saving this change, tell Git about it and commit:

git add .gitignore
git commit -m "Add /oldtest to .gitignore"

2. Remove files from Git Tracking (using –cached)

Now it’s time to remove the files that Git was tracking but we no longer want in the repository. The key here is to remove them from Git’s tracking without deleting them from your local computer. That’s why the –cached flag is so important:

git rm -r --cached /oldtest 
  • git rm -r: This command is used to remove a specified folder and its contents. The -r flag stands for recursive, meaning it removes everything inside the folder.
  • --cached: This is the magic part! This flag makes sure the files are only removed from the Git repository, but they stay in your local working directory. So, the /oldtest folder won’t be deleted from your computer, only from GitHub.

After running this command, Git will show you what was removed.

3. Commit the changes and push to GitHub

Finally, to reflect these removal changes on your GitHub repository, commit them and then push:

git commit -m "Remove /oldtest folder from repository"
git push origin your-branch-name

Don’t forget to replace your-branch-name with the name of your project’s main branch (usually main or master).

Conclusion

Once you’ve followed these steps, the unwanted /oldtest folder will be gone from your GitHub repository. You can use these same steps for similar situations to keep your repo clean.

Remember, a good .gitignore file is the foundation for a tidy GitHub repository!

Table Of Contents

3 min read
© 2025. Design & development by Ozay
Hosted with Cloudways