0

This is a follow up to my previous post here. I'm trying to remove a large file that I committed to git. Based on the suggestion given in my previous post, I tried

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch folder/Unconfirmed 866711.crdownload" --prune-empty --tag-name-filter cat -- --all

Following the above command, I tried to push all the changes

git push origin --force --all

However, I got the same error that was displayed before using filter-branch

remote: error: File folder/Unconfirmed 866711.crdownload is 486.30 MB; this exceeds GitHub's file size limit of 100.00 MB

Alternatively, I also tried

git add --all

git filter-branch -f --index-filter "git rm --cached --ignore-unmatch folder/Unconfirmed 866711.crdownload" HEAD

But, I get the following

Cannot rewrite branches: Your index contains uncommitted changes.

I'm not sure if I have missed any command or flags. Any suggestions?

Natasha
  • 1,111
  • 5
  • 28
  • 66

1 Answers1

3

Cannot rewrite branches: Your index contains uncommitted changes.

You have uncommitted changes in your working directory i.e changes staged for a commit (check git status output) . Either commit those changes if you wish to or preserve those uncommitted changes by using stash and apply them once you have executed the filter-branch command.

If you don't want the uncommitted changes, then you can perform a hard reset.

git reset --hard HEAD


As @torek mentioned in the comment below and as per GitHub help page, it is not recommended to use stash before filter-branch

Warning: If you run git filter-branch after stashing changes, you won't be able to retrieve your changes with other stash commands. Before running git filter-branch, we recommend unstashing any changes you've made.

So instead commit the changes if you want to preserve the uncommitted changes. Then proceed with the filter-branch command.


Your filter-branch command, is missing single quotes (to account for the space in the name of the file to be deleted)
git filter-branch -f --index-filter "git rm --cached --ignore-unmatch 'folder/Unconfirmed 866711.crdownload'" HEAD
Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • 1
    I recommend *not* using `git stash` with `git filter-branch`. The filter-branch command can operate on all refs, including the stash ref, and can destroy stashes in the process. – torek Dec 19 '19 at 18:08
  • @torek, is it possible to exclude stash ref from being overwritten in `filter-branch` ? – Saurabh P Bhandari Dec 19 '19 at 18:13
  • Yes: if you specify a list of refs to rewrite (e.g., `git filter-branch -- branchA branchB`) it rewrites only those branches. But I think it is easier and more practical to just commit, then filter, then back out the last commit. – torek Dec 19 '19 at 18:23
  • @SaurabhPBhandari As you suggested, I committed all the changes followed by filter-branch. I get the following, `WARNING: Ref 'refs/heads/master' is unchanged WARNING: Ref 'refs/remotes/origin/master' is unchanged WARNING: Ref 'refs/stash' is unchanged` . Could you please suggest what should be the next step? Can I push the changes to the repository? – Natasha Dec 20 '19 at 08:16
  • @Natasha, The warning suggests that your command didn't change any refs i.e it wasn't successful. – Saurabh P Bhandari Dec 20 '19 at 08:18
  • @Natasha, Have you followed [this](https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository#using-filter-branch) ? – Saurabh P Bhandari Dec 20 '19 at 08:21
  • @SaurabhPBhandari Thanks a ton! I think it worked this time with the single quotes, "Ref 'refs/heads/master' was rewritten WARNING: Ref 'refs/remotes/origin/master' is unchanged Ref 'refs/stash' was rewritten" . I get one warning now. can I go ahead and push the changes after adding the path of the large file to .gitignore and commiting the change? – Natasha Dec 20 '19 at 08:44
  • @Natasha, yes, you can do that, The warning suggests that other than `refs/heads/master` all other refs remain unchanged – Saurabh P Bhandari Dec 20 '19 at 08:51
  • Just ran `git filter-branch --index-filter "git rm --cached --ignore-unmatch 'public/long file name.webarchive'" HEAD` and got back `WARNING: Ref 'refs/heads/35-create-shippet-in-new' is unchanged`. Do I need to do something else?Trying to get rid of large files I inadvertently added years ago. – Greg Jan 04 '21 at 20:20