5

I pushed my local repository to GitHub. In the process of committing my code, I forgot to create a .gitignore file. As a result, I have committed and subsequently pushed some folders and files that I didn't want on GitHub (or in my local repository, for that matter).

How can I apply .gitignore now, so that I can remove some undesired folders and files going forward?

Dan McClain
  • 11,780
  • 9
  • 47
  • 67
Rookian
  • 19,841
  • 28
  • 110
  • 180

4 Answers4

6

You can git rm <unnecessary file and folder names> then add them to your .gitignore file, which will remove them from that commit forward. The issue is that they will remain in the history unless you alter the earlier commits. If there is no sensitive data in those files, I'd say leave the history as is. If you need to remove sensitive data from your repository history, see GitHub's help article on the subject

Dan McClain
  • 11,780
  • 9
  • 47
  • 67
3

First, delete the bin directory added to the repo by accident. You may refer here: http://help.github.com/remove-sensitive-data/

Then add .gitignore and commit, and then push to github.

Vivodo
  • 1,542
  • 2
  • 17
  • 31
2

From memory you have to remove it and re-add it. Git ignore will then take effect.

Answered already?

Community
  • 1
  • 1
Jack
  • 10,313
  • 15
  • 75
  • 118
2

If this is your private repo and you haven't published it yet, use filter-branch to edit history:

git filter-branch --index-filter '
  git rm -r --cached --ignore-unmatched bin;
' --all

Then, add/edit your .gitignore file:

>> .gitignore echo bin
git add .gitignore
git commit -m 'add gitignore files'

Since you have already published your history, it's best practice not to rewrite it. Just remove the bin directory and add a .gitignore file

git rm -r bin
# add .gitignore
git commit -m 'remove and ignore bin folder'
knittl
  • 246,190
  • 53
  • 318
  • 364
  • is an approach of automatically delete all the folders/files using gitignore? Do I really have to execute your commands for each file/folder I want to delete? – Rookian Nov 13 '11 at 15:01
  • You can simply do `git rm -r --cached --ignore-unmatched file1 folder1 file2 folder2 folder3 *bak test-*`. Just be sure, that you understand the implications of history rewriting, if you go the `filter-branch` route! – knittl Nov 13 '11 at 15:05