How to remove file and folder from git?

How to remove file and folder from git?

GIT is great, it has made collaboration with other developers so easy, I can’t thank GIT enough. But GIT is vast and not every command remains on my mind. I find myself googling over and over again to get that right GIT command that can solve my problem.

Recently when working on one of the project that had just started, I accidentally pushed IDE folder to remote repository and I was there googling again, so I thought, why not write blog for this? I can always come straight to my blog if this happens again and I can also help my fellow developers this way, right?

All sensitive information and IDE related folders should be added to gitignore so they are not tracked by git. You may already know this, but it doesn’t help if your file or folder is already in the remote repository. Today we will learn how we can remove files or folders from remote repository. Let’s get started!

Remove file or folder from remote repo only

# Remove a single file
git rm --cached password.txt

# Remove a single folder
git rm --cached -rf .idea

Remove file or folder from both remote repo and local

# Remove a single file
git rm password.txt

# Remove a single folder
git rm -rf .idea

Add file or folder to gitignore

After removing the file or folder, we shouldn’t forget to add them to gitignore before we commit and push to the repo again. Or we will be back to the start of blog removing those again!

Straight and sweet. Any confusions? Have a better solution? Please comment below, it’s never a bad idea to have a healthy conversation 😉

References: Remove file or folder only from remote repo

Image Credits: Cover Image by Mahmudul Hasan Shaon from Unsplash