Remove directory from remote repository after adding them to .gitignore

GitGithubGitignore

Git Problem Overview


I committed and pushed some directory to github. After that, I altered the .gitignore file adding a directory that should be ignored. Everything works fine, but the (now ignored) directory stays on github.

How do I delete that directory from github and the repository history?

Git Solutions


Solution 1 - Git

The rules in your .gitignore file only apply to untracked files. Since the files under that directory were already committed in your repository, you have to unstage them, create a commit, and push that to GitHub:

git rm -r --cached some-directory
git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master

You can't delete the file from your history without rewriting the history of your repository - you shouldn't do this if anyone else is working with your repository, or you're using it from multiple computers. If you still want to do that, you can use git filter-branch to rewrite the history - there is a helpful guide to that here.

Additionally, note the output from git rm -r --cached some-directory will be something like:

rm 'some-directory/product/cache/1/small_image/130x130/small_image.jpg'
rm 'some-directory/product/cache/1/small_image/135x/small_image.jpg'
rm 'some-directory/.htaccess'
rm 'some-directory/logo.jpg'

The rm is feedback from git about the repository; the files are still in the working directory.

Solution 2 - Git

I do this:

git rm --cached `git ls-files -i --exclude-from=.gitignore` 
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

Which will remove all the files/folders that are in your git ignore, saving you have to pick each one manually


This seems to have stopped working for me, I now do:

 git rm -r --cached . 
 git add .
 git commit -m 'Removed all files that are in the .gitignore' 
 git push origin master

Solution 3 - Git

As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

To remove folder/directory only from git repository and not from the local try 3 simple steps.

Steps to remove directory

git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master

Steps to ignore that folder in next commits > To ignore that folder from next commits make one file in root named .gitignore > and put that folders name into it. You can put as many as you want

.gitignore file will be look like this

/FolderName

remove directory

Solution 4 - Git

Blundell's first answer didn't work for me. However it showed me the right way. I have done the same thing like this:

> for i in `git ls-files -i --exclude-from=.gitignore`; do git rm --cached $i; done
> git commit -m 'Removed all files that are in the .gitignore'
> git push origin master

I advise you to check the files to be deleted first by running the below statement:

git ls-files -i --exclude-from=.gitignore

I was using a default .gitignore file for visual studio and I noticed that it was removing all log and bin folders in the project which was not my intended action.

Solution 5 - Git

Note: This solution works only with Github Desktop GUI.

By using Github Desktop GUI it is very simple.

  1. Move the folder onto another location (to out of the project folder) temporarily.

  2. Edit your .gitignore file and remove the folder entry which would be remove master repository on the github page.

  3. Commit and Sync the project folder.

  4. Re-move the folder into the project folder

  5. Re-edit .gitignore file.

That's all.

Solution 6 - Git

>This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored. > > Can't use --exclude-from=.gitignore anymore :/ - Here's the updated method: > > General advice: start with a clean repo - everything committed, nothing pending in working directory or index, and make a backup!

#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"

#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *

#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.

git commit -m "re-applied modified .gitignore"

#other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED

If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.

Solution 7 - Git

The answer from Blundell should work, but for some bizarre reason it didn't do with me. I had to pipe first the filenames outputted by the first command into a file and then loop through that file and delete that file one by one.

git ls-files -i --exclude-from=.gitignore > to_remove.txt
while read line; do `git rm -r --cached "$line"`; done < to_remove.txt
rm to_remove.txt
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

Solution 8 - Git

If you're using the technique described by Blundell (remove everything then add everything) on a windows machine you may hit an issue with all the files being modified because the line endings change. To avoid that use the following commands:

git rm -r --cached .
git config --global core.autocrlf false
git add --renormalize .
git add .

This removes all files from the index, configures git to not change line endings, renormalises line endings and stages all the files you just removed except those specified in the .gitignore

Then git commit

Reference: https://stackoverflow.com/questions/68641723/how-to-remove-files-from-repository-listed-in-gitignore-without-changing-whites

Solution 9 - Git

If you're working from PowerShell, try the following as a single command.

PS MyRepo> git filter-branch --force --index-filter
>> "git rm --cached --ignore-unmatch -r .\\\path\\\to\\\directory"
>> --prune-empty --tag-name-filter cat -- --all

Then, git push --force --all.

Documentation: https://git-scm.com/docs/git-filter-branch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjanwView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitBlundellView Answer on Stackoverflow
Solution 3 - GitSuresh KariaView Answer on Stackoverflow
Solution 4 - GitOncel Umut TURERView Answer on Stackoverflow
Solution 5 - GitefkanView Answer on Stackoverflow
Solution 6 - GitgoofologyView Answer on Stackoverflow
Solution 7 - GitChris AelbrechtView Answer on Stackoverflow
Solution 8 - GitColinView Answer on Stackoverflow
Solution 9 - GitShaun LuttinView Answer on Stackoverflow