Reputation: 14165
I added over 9000 photos by accident to my project folder. And committed them. Then deleted them from the disk. Committed.
Now I try to push changes to the git server. But it takes too long and tries to send 12 GB of data.
I checked files size on the disk and saw that really .git
folder takes 12 GB.
How to delete photos from there? I tried git rm
, but fails:
❯ git rm public/photos
fatal: pathspec 'public/photos' did not match any files
Because I already deleted them from the disk, but they are still in .git
folder.
I tried to add public/photos
to .gitignore
:
public/photos/
*.zip
But no result.
Of course, I could hard reset head
to the moment when I did not have so many junk photos in my project. But since that time I committed many times and made a lot of changes in code.
Upvotes: 147
Views: 362306
Reputation: 76887
In your case, use git filter-branch
instead of git rm
.
git rm
will delete the files in the sense that they will not be tracked by git any more, but that does not remove the old commit objects corresponding to those images, and so you will still be stuck with pushing the earlier commits which correspond to 12GB of images.
The git filter-branch
, on the other hand, can remove those files from all the previous commits as well, thus doing away with the need to push any of them.
Use the command
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch public/photos' \
--prune-empty --tag-name-filter cat -- --all
After the filter branch is complete, verify that no unintended file was lost.
Now add a .gitignore rule
echo public/photos >> .gitignore
git add .gitignore && git commit -m "ignore rule for photos"
Now do a push
git push -f origin branch
Check this, this and this for further help. Just to be on the safer side, I would suggest you create a backup copy of the repo on your system before going ahead with these instructions.
As for your original error message, it is happening because you already untracked them using git rm
, and hence git is complaining because it can't remove a file it isn't tracking. Read more about this here.
Upvotes: 138
Reputation: 530
I had a similar issue with the following error message (Google brought me here):
error: pathspec 'elements (conflicted copy 2013-08-06)' did not match any file(s) known to git
I tried all of the solutions described here, but none worked for me. I had two files that were simply not there anymore, but git was still showing me the files and it was impossible to remove them. They were created thanks to a conflict with Dropbox.
Steps that worked for me:
git stash
This resulted in the two files existing again in the folder
Git will now show the renamed files as untracked, but the old files still as deleted
Changes not staged for commit: (use "git add/rm ..." to update what will be committed) (use "git restore ..." to discard changes in working directory)
deleted: "elements (conflicted copy 2013-08-06)"
deleted: "layout (conflicted copy 2013-08-06)"Untracked files: (use "git add ..." to include in what will be committed)
elements_dupe
layout_dupe
git add -A
Suddenly, git status displayed, that the previous files were renamed
Changes to be committed: (use "git restore --staged ..." to unstage)
renamed: "elements (conflicted copy 2013-08-06)" -> elements_dupe renamed: "layout (conflicted copy 2013-08-06)" -> layout_dupe
git commit -m 'renamed duplicate files'
Afterwards, it was very easy to remove them by simply deleting the files and commit again.
Upvotes: 0
Reputation: 483
I had this error when I aborted an upload which was taking too long, to fix it I renamed the file, commit, renamed it back, commit.
Upvotes: 0
Reputation: 681
I got the same error while I wanted to delete cached node_modules
dir.
instead of
git rm -r -f --cached node_modules
I had to use
git rm -r -f --cached **/node_modules
, because the dir was not part of the root dir.
Upvotes: 0
Reputation: 405
Sometimes it's happens when you are not tracking your file add your file with git add untracked file name
, after this simply do git rm -r file name
Upvotes: 2
Reputation: 7249
Just for reference, I noticed that I had some files with permissions 000, maybe they were created with sudo , I don't know, but after changing your permissions to 644 (I needed sudo for this operation), the problem was solved
sudo chmod 644 vendor/symfony/yaml
and the commit result :
diff --git a/vendor/symfony/yaml b/vendor/symfony/yaml
deleted file mode 160000
index 212a27b7..00000000
--- a/vendor/symfony/yaml
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 212a27b731e5bfb735679d1ffaac82bd6a1dc996
diff --git a/vendor/symfony/yaml b/vendor/symfony/yaml
new file mode 100644
index 00000000..20a0b9d0
--- /dev/null
+++ b/vendor/symfony/yaml
@@ -0,0 +1 @@
+Subproject commit 212a27b731e5bfb735679d1ffaac82bd6a1dc996
Upvotes: 0
Reputation: 2063
A very simple answer is.
Step 1:
Firstly add your untracked files to which you want to delete:
using git add .
or git add <filename>
.
Step 2:
Then delete them easily using command git rm -f <filename>
here rm=remove and -f=forcely.
Upvotes: 40
Reputation: 1
I had a duplicate directory (~web/web) and it removed the nested duplicate when I ran rm -rf web
while inside the first web folder.
Upvotes: -2
Reputation: 8979
To remove the tracked and old committed file from git you can use the below command. Here in my case, I want to untrack and remove all the file from dist
directory.
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch dist' --tag-name-filter cat -- --all
Then, you need to add it into your .gitignore
so it won't be tracked further.
Upvotes: 7
Reputation: 588
This chains work in my case:
git rm -r WebApplication/packages
There was a confirmation git-dialog. You should choose "y" option.
git commit -m "blabla"
git push -f origin <ur_branch>
Upvotes: 1
Reputation: 3631
git stash
did the job,
It restored the files that I had deleted using rm
instead of git rm
.
I did first a checkout of the last hash, but I do not believe it is required.
Upvotes: 1
Reputation: 481
Step 1
Add the file name(s) to your .gitignore
file.
Step 2
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch YOURFILE' \
--prune-empty --tag-name-filter cat -- --all
Step 3
git push -f origin branch
A big thank you to @mu.
Upvotes: 14