remove gitignore files in git repository

How to Remove Git Ignore Files from Git Repository

Adding a .gitignore file to your repository allows you to specify which files you don’t want to be tracked in your git repository. The files & folders listed in .gitignore file will not be tracked by git program, after you add the file. But that does not mean these files will be removed from your repository folder. If you want to remove these files, you will need to do it manually. In this article, we will learn how to remove git ignore files from git repository.


How to Remove Git Ignore Files from Git Repository

Here are different ways to remove git ignore files from repository. Execute the following commands from within git repository, that is, after navigating to the repository folder. Also, if the following commands give you ‘permission denied’ error, then try adding a ‘sudo’ keyword at the beginning of these commands, in Linux.


1. Remove a few files

You can easily remove gitignore files using the git rm command as shown below. Replace file1, file2, dir/file3 with files and directories that you want to remove from repository.

$ git rm --cached file1 file2 dir/file3


2. Remove Many Files

If there are too many files to be removed, you can use the following command. It basically lists all files to be excluded as per .gitignore file and deletes them.

$ git rm --cached `git ls-files -i -c --exclude-from=.gitignore`

The above command works on Linux systems. If the above command does not work for you, you can try the following command instead.

$ git ls-files -i -c --exclude-from=.gitignore | xargs git rm --cached  

It creates separate rm commands for each file & folder mentioned in .gitignore file, using xargs command.

The above commands may not work on Windows. For Windows systems, use the following command instead.

# git ls-files -i -c --exclude-from=.gitignore | %{git rm --cached $_}

But please note, none of the above commands will remove these files from your repository’s history. All their changes will continue to remain in the git history.

After you remove the files & folders mentioned in .gitignore, you can run the following command to commit the changes.

$ git add .
$ git commit -m "Drop files from .gitignore"

In this article, we have learnt how to remove files listed in .gitignore file of your repository.

Also read:

How to Stop Python Code After Certain Amount of Time
How to Convert Bytes to String in Python
How to Store Output of Cut Command in Linux
How to Use Shell Variables in Awk Script
How to Setup SSH Keys in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *