Skip to content

Instantly share code, notes, and snippets.

@guojianwei001
Last active September 2, 2022 09:35
Show Gist options
  • Save guojianwei001/11603e0dc2a361a3a40c4e0372e0bcd8 to your computer and use it in GitHub Desktop.
Save guojianwei001/11603e0dc2a361a3a40c4e0372e0bcd8 to your computer and use it in GitHub Desktop.
Purging a file from your repository's history, i.e. clean all the commits of a file

References:

1. Solution one: git rebase

  • This solution applies if the affected commits count is small.
  • refence
> git rebase -i ABC~
> change pick to e for commits to update
> make changes what you want
> git commit -a --amend
> git rebase --continue
...
> git push -f

2. Solution two: bfg

* cdc6249 (HEAD -> master) commit 3: only change appsettings.json
* 9ada1e9 commit 2: changes only for 1.txt
* 5b0b22e initial commit

> bfg --delete-files appsettings.json

* a585173 (HEAD -> master) commit 3: only change appsettings.json
* 49c17f3 commit 2: changes only for 1.txt
* 42c6840 initial commit

### obviously, except the head commit, commits of appsettings.json are removed completed. commits Ids are regenerated.

3. Solution three: git filter-repo

  • install filter-repo manually on windows
  • install python 3.x(x>=5), here install use 3.10
  • download git_filter_repo.py file on local machine from repo
  • commits comparison before and after running filter-repo, as follows: appsettings.json is the file being removed.
* 2f31484 (HEAD -> master) commit 6: only change 1.txt
* aecc3c1 commit 5: only change appsettings.json
* af9fa6a commit 4: only change 1.txt
* a585173 commit 3: only change appsettings.json
* 49c17f3 commit 2: changes only for 1.txt
* 42c6840 initial commit

> python ../git_filter_repo.py --invert-paths --path appsettings.json --force ## execting

7ae363a (HEAD -> master) commit 6: only change 1.txt
9e84a8e commit 4: only change 1.txt
9ada1e9 commit 2: changes only for 1.txt
5b0b22e initial commit

### obviously, commits of appsettings.json are removed completed. commits Ids are regenerated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment