Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply gitignore afterwards?

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?

like image 870
Rookian Avatar asked Nov 13 '11 14:11

Rookian


People also ask

How do you add to Gitignore after commit?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Can I add Gitignore after creating repo?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.


2 Answers

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

like image 82
Dan McClain Avatar answered Oct 09 '22 00:10

Dan McClain


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.

like image 43
Vivodo Avatar answered Oct 09 '22 01:10

Vivodo