Reputation: 536
I have been trying to remove some files from the git version control system so that they are not pushed on commits.
Both git rm --cached filename and git rm --force --cached filename not working for several files. Basically the removed files do get pushed to github (not when I change the files in question but only when I edit and commit other files in the project).
Furthermore, for a couple of files I get fatal: pathspec did not match any files error when the files do exist in the path for git rm command.
Something is foobared here but I don't know how to reset everything safely. Can you please help.
Upvotes: 5
Views: 13772
Reputation: 58647
It is not enough to remove a file from the index. You must also create a commit out of that index to the branch you are on and then push that out to the remote repository where you want this change to appear. The removes will not get pushed to github if all you do is git rm ...
and then a git push
without a commit.
After you perform a git rm --cached
, the files are removed from the index, while remaining in the working tree.
If you then run git diff
, you see nothing: that is because it is comparing the working tree to the branch tip. To see the staged removal, use git diff --cached
. That is the diff between the branch tip and the index. That is what will be committed when you next invoke git commit
.
Your removes are being pushed only when you commit other files because commits are necessary for removal. When you make other changes and commit those, those commits are picking up the previously staged removals.
If you have accidentally combined an off topic file removal of a file x
with edits to a file y
in the same commit, you can use git reset HEAD^ -- x
to create a staged index which reverses the removal of x
. Then commit that. Or use commit --amend
if you didn't publish the bad commit yet. The amended commit now just has the y
edits and x
is once again a tracked file. Now re-do the removal with rm --cached x
and make a new commit for that.
Upvotes: 3
Reputation: 1791
Try these commands:-
git reset filename
git reset HEAD filename
They will remove the files from staging area.
Upvotes: -1