My .gitignore
file wasn't working, so I followed this question.
And now I have bunch of files that I need to commit, in order to proceed. I made a mistake and committed those, because I couldn't find a way to get rid of those.
Is there any way that I can revert all these actions? I can't do reset
cause I now I get error:
error: The following untracked working tree files would be overwritten by checkout
.
Stupid stupid mistake, and I can find solution for this.
If you've run only git rm -r --cached , try doing a git reset HEAD . from within your repo root. If you did a git commit -m "msg" after doing a git rm -r --cached , i.e., you committed the changes, then do git reset HEAD~1 to undo your last commit.
By default, the git rm command deletes files both from the Git repository as well as the filesystem. Using the --cached flag, the actual file on disk will not be deleted.
The Git rm –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you'll still have a copy of the file locally. The file will be removed from the index tracking your Git project.
git rm —cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD — file will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting.
If you've run only git rm -r --cached
, try doing a git reset HEAD .
from within your repo root.
If you did a git commit -m "msg"
after doing a git rm -r --cached
, i.e., you committed the changes, then do git reset HEAD~1
to undo your last commit.
Git works based on file caching so if you removed everything from the cache you can just reverse the whole process by executing .This will add back the files that were being tracked and tell which ones have been modified since the last commit .
> git add .
If you want git to make git "retrack" your files, that once where removed by .gitignore or by
git rm --cached
you can do it so by using:
git add -f <files>
After that if you want to remove files from the staged area you can use:
git restore --staged <files>
Running git checkout HEAD path/to/file
will not work if the file is removed from the cache.
What worked for me is to move to a new branch by running the command:
git checkout -b newBranch
If you want to revert it for only a particular file you can do
git restore --staged <file>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With