0

I am trying to do my first commit and push to github. Originally my angular/cache was not in the gitignore file. Now I have added it and tried to commit and push but it keeps including the angular/cache dir ie it keeps trying to add previously tracked stuff I think.

Things I have tried:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"
git push -u origin master

I have also tried git update-index --assume-unchanged .angular/cache

Everytime I the filed keep adding

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
pilotman1
  • 39
  • 6
  • Note: I have tried the suggestions in https://stackoverflow.com/questions/1274057/how-can-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitign – pilotman1 Dec 30 '21 at 21:07
  • Did you push something? Have you commit something? If you have push anything, you must untrack the specific file where you want to add to gitignore. How this work, look on this [answer](https://stackoverflow.com/a/63093031). If you don’t have pushed anything, you must unstage the file where you want to add to gitignore with `git reset `, add to gitignore, commit and push. – SwissCodeMen Dec 30 '21 at 22:25
  • If you already committed the files, they're in that commit. They are in that commit *forever*. You can make a new commit that lacks the files, but you can't remove the files from the existing commit. If the bad commit was your very first commit, this particular error can become annoyingly painful to work around. There are workarounds, but before getting to them, we should find out how many commits you have: add the output of `git log --all --decorate --oneline --graph` to your question. – torek Dec 31 '21 at 01:54
  • Does this answer your question? [How can I make Git "forget" about a file that was tracked, but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-can-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitign) – R. Richards Jan 01 '22 at 19:14

2 Answers2

1

Check the rules in the .gitignore file and try running the following commands. While your use uses the -r flag, in this case the -rf flag is used.

git rm -rf --cached .
git add .
git commit -m "gitignore is now working"
git push

The flags applied to the git rm command are described as follows:

  • -f Overrides the security check for making sure that the files in the HEAD correspond to the content of the staging index and working directory.
  • -r This is a shorthand for 'recursive'. The git rm removes a target directory and its whole content when working in recursive mode.

References
Sercan
  • 4,739
  • 3
  • 17
  • 36
  • So I tried the commands and after the commit the message 'On branch master nothing to commit, working tree clean' Then when I push ''git push --set-upstream origin master" it still pushes everything maybe because its the first pust for this repo? For information: I added the gitignore rule after I first tried to push (which failed) then I added the rule which may be causing this? – pilotman1 Dec 30 '21 at 21:25
1

A .gitignore file only prevents untracked files from being added to the index [1], so that if you run git add the files listed there will not be added. It does not remove already added files from the index ! Why removing the files from the index does not help I have no idea, but it should work if you rm -rf the .git folder and re-init a new repository (i.e. start your git repository from scratch).

TheEagle
  • 5,808
  • 3
  • 11
  • 39