简体   繁体   中英

Deleting a directory from git history on a specific branch

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:

  1. created a branch off of master, let's call it 'my-branch'.
  2. did some work in 'my-branch' including an upgrade that included a cache directory (.angular/cache/...).
  3. ran these commands - git add. ; git commit -m "some message"; git push origin head
  4. the push failed since the files were too large for git.
  5. 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)
  6. 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...).
  7. 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.
  8. 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:

  1. Remove the folder from the history of "my-branch" not touching any other branches or master.
  2. '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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM