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?
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.
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
Note: This solution works only with Github Desktop GUI.
By using Github Desktop GUI it is very simple.
Move the folder onto another location (to out of the project folder) temporarily.
Edit your .gitignore
file and remove the folder entry which would be remove master repository on the github page.
Commit and Sync the project folder.
Re-move the folder into the project folder
Re-edit .gitignore
file.
That's all.
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
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
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.
As per my Answer here: How to remove a directory from git repository?
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
Source: Stackoverflow.com