5

I want to remove some files/ folder from git history according to git documentation, but it fails due to unstaged changes. It is not clear to me what unstaged changes there could be as i did git add --all , commit & push before. note that the to-be-deleted folder somefolder/ does not exist in HEAD anymore. It was moved on earlier commits, but it's old versions need to be purged from the repo.

~$ git add --all
~$ git commit -m "trying to fix 'Cannot rewrite branches: You have unstaged changes'"
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
~$ git push
Everything up-to-date
~$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
~$ git filter-branch --force --index-filter   "git rm --cached --ignore-unmatch somefolder/"   --prune-empty --tag-name-filter cat -- --all
Cannot rewrite branches: You have unstaged changes.
~$ 

how do i fix this so i can run the last command successfully?

Dr-Nuke
  • 368
  • 3
  • 11

1 Answers1

2

First, make sure to use the latest Git (2.26), as there was a bug in older Git versions regarding git filter-branch.

Second, you might want to clone your repo as a bare repository, and do the filtering there. That would prevent any unstaged changes, since a bare repository has no working tree.

But thrid, don't use the obsolete BFG or git filter-branch for this.
Use new tool git filter-repo (directly in your regular local repository, although a backup is always a good idea before those kind of filtering)

Use a path filtering:

git filter-repo --path somefolder/ --invert-paths

That will remove somefolder/ from the Git repository history.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your comprehensive answer. I had some trouble like `git-filter-repo` requiring the dash before `filter`, and subsequent pushing got rejected `because the remote contains work that you do`. i could fix that with the `--force` flag. however i finally managed to remove that specific folder from the git history. I also got `fatal: refusing to merge unrelated histories` when pulling the changes to my original local repo. will fix that, too.... – Dr-Nuke Apr 05 '20 at 10:11