Green Screen

【git】履歴を無かったことにする。

戻る

▲▲CAUTION▲▲
※ツールgit-filter-repoで過去git履歴を消す場合、物理ファイルも無くなるので、
絶対にソース一式のバックアップを取ること
※あと、ローカルだけで編集済(git status で modifiedと出る状態のソースなど)も、
全部ロールバックされるので、操作したいファイル以外は全てcommitしてリモートリポジトリと
同期済にしてから実行すること。

※このツールgit-filter-repoは、各venvとは独立した所に入れる必要あり。各アプリの依存関係リストに、無関係なツールが入るのは、
紛らわしくて嫌でしょう。
(ubuntuでは、システム素のpython側に入れてもOK)

1)pipxを導入
sudo apt update
sudo apt get install pipx

2)venv無しでpipx実行
pipx install git-filter-repo

3)確認
git filter-repo --version

■特定のファイルを「無かったことにする」方法
1)git filter-repo --path xyz/black_hisotory_file.py --invert-paths
これだけだと、ちゃんと「安全機構」が機能して中断される
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
(expected freshly packed repo)
Please operate on a fresh clone instead. If you want to proceed
anyway, use --force.
最後に--forceをつけると強制できる
git filter-repo --path xyz/black_hisotory_file.py --invert-paths --force

2)リモートリポジトリの再設定
git filter-repoを使うと、いったんローカルとリモートが切断される。
git remote add origin https://github.com/12345/exisis_repo.git

3)強制プッシュ(1回のみ)
git push -f origin master

4)git hub上で変更反映されてたらOK

5)サーバー側での処置
普通にgit pull するとエラー中断する。
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

フェッチをやりなおし、強制的に切り替える
git fetch origin
git reset --hard origin/master
HEAD is now at 番号 "commitのメッセージ" がでたらOK

作成日: 2026年6月29日10:57

更新日: 2026年6月29日10:57

タグ

IT

戻る