The issue: There is a folder in my GitHub repo, in a branch called 'my-branch' and it needs to be removed. It is not the last commit.
Details of issue:
- created a branch off of master, let's call it 'my-branch'.
- did some work in 'my-branch' including an upgrade that included a cache directory (.angular/cache/...).
- ran these commands - git add. ; git commit -m "some message" ; git push origin head
- the push failed since the files were too large for git.
- realizing that folder should not have been pushed in the first place, I updated the .gitignore and did a subsequent push (git add .gitignore; git commit -m "added /.angular/cache to .gitignore" ; git push origin head)
- The problem is that the history of 'my-branch' still contains the directory '/.angular/cache' and I got hit with bot security scanner saying that the folder is still in some commit (84bd63...).
- I tried to run 'bfg-repo-cleaner' tool to delete that folder completely from the history, but failed to push since develop and master branches are protected.
- Now I see the folder is still being picked up by the bot scanner, but above it, it says that the file doesn't belong to any branch.
Desired Outcome:
- Remove the folder from the history of "my-branch" not touching any other branches or master.
- 'Master' and 'develop' branch are protected and I do not have access to write to them directly, which is why the 'bfg-repo-cleaner' failed to push.
PLEASE HELP
CodePudding user response:
If the branch is straight (at least, since adding that directory), it's rather simple to correct
git rebase -i the-commit-where-the-directory-was-added~ # do _not_ skip the pig tail, it has to biñe there
# the commit should be the first in tbe list
# change pick for edit in that commit only
# save exit
# rebase will stop right after applying that commit
git rm --cached the-directory
# consider adding the directory to .gitignore and add it also so it's not added by mistake later
git commit --amend --no-edit
git rebase --continue
When it finishes running, you should have a branch without the directory