Emilio
Emilio

Reputation: 1354

git: Delete files and directories from all history but keep all branches (not just the master branch)

I tried this tutorial to delete some sensitive files and directories from my Git history. However, at the end, my new repository only had 1 branch: the master branch.

How is possible to delete files and directories from Git history while keeping all branches?

Thank you!

commands I used (from the tutorial):

cd /tmp 

git clone https://MY_GIT_REPOSITORY.git workingrepo

cd workingrepo

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do

git branch --track ${branch##*/} $branch 

done 

git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch FILE_LIST' --prune-empty -f -- --all 

rm -rf .git/refs/original/ 

git reflog expire --expire=now --all 

git gc --aggressive --prune=now

Upvotes: 0

Views: 62

Answers (1)

torek
torek

Reputation: 488193

From what you've posted, that should have worked. The:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
    git branch --track ${branch##*/} $branch
done

sequence should create a branch for each branch in the original repository. If you examine the set of branches afterward, you should see the right names. (This method is a bit clumsy compared to just using git clone --mirror for the initial clone, but sometimes there are reasons to avoid --mirror, which makes a bare clone.)

The filter-branch operation (lines split for posting purposes here):

git filter-branch --tag-name-filter cat \
    --index-filter 'git rm -r --cached --ignore-unmatch FILE_LIST' \
    --prune-empty -f -- --all 

should then operate on all branches (--all at the end); the index filter will remove a file named FILE_LIST (presumably this is short for the actual list of files), and you should see a lot of progress messages plus a list of branch names that filter-branch updated.

You can examine the branch names again at this point; they should be the same names as before (though the commit hashes to which they point may, and usually will, differ).

The remaining commands do some cleanup. The --aggressive is unnecessary (see Linus Torvalds' explanation here) but using git gc or re-cloning are appropriate for discarding the unwanted objects. These will have no effect on branch names, so those two particular points are where to check why something has gone wrong.

Upvotes: 2

Related Questions