0

I often forgot to add node_modules or components to .gitignore and they wind up in my git commit and push to github (it takes forever to upload). Is there anyway to remove all instances of these folders from every commit and keep them in the working directory?

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

3 Answers3

2

Simple way to remove from current revision:

Use git -rm -r --cached

The --cached option will keep the file or directory in your local working directory, and remove it from git.

From git help rm:

--cached
     Use this option to unstage and remove paths only from the index. 
     Working tree files, whether modified or not, will be left alone.

See https://www.kernel.org/pub/software/scm/git/docs/git-rm.html

Remove from all revisions:

If you really need to remove your files/directories from all revisions, take a look at this thread and the git help page dedicated to this.

Remove sensitive files and their commits from Git history

https://help.github.com/articles/remove-sensitive-data

Community
  • 1
  • 1
jszobody
  • 28,495
  • 6
  • 61
  • 72
1

Use either git -rm -r --cached like jszobody suggested, or do git reset HEAD which is a bit less typing and the way suggested by git status to remove files from a commit (before actually calling git commit). That is called unstaging.

However, after git commit there is no easy way to remove those folders. If the last commit introduced changes that you do not want to keep, modify your local copy to be in the state you want it to be, run git add . and do a git commit --amend. If the unwanted changes were introduced in a commit further behind or in multiple commits, you must rebase via git rebase -i. I suggest you to read the man page about it or look for some tutorials.

If you already pushed the changes to github, you must run git push -f after your rebase or afer your amend. If you are working in team, rebasing on yet pushed commits is a bad idea as it will give your commits different hashes than those of the rest of your team.

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36