はじめに
git fetchでリモートリポジトリから大量に読み込んでしまった全てのリモートブランチの履歴を、ローカルリポジトリの履歴から消して、読込前の状態に(なるべく)戻します。
環境準備
Gitの環境を導入してGitHubにpushするまでの手順は、一通り終わっている状態から、始めます。
一からGitの環境を導入してGitHubにpushするまでの手順は、下記参照:
Git for Windowsを使用:
Google Colaboratoryを使用:
ローカルリポジトリを準備
(下記のコマンドは、Colab上で試行)
%%bash
# ローカルのリポジトリを作成
git init
# (例えば、古い長い)過去履歴のあるリモートリポジトリ(GitHub)に接続
git remote add origin "https://github.com/DL-from-Scratch/test1"
# https://{USER}:{ACCESS_TOKEN}@github.com/{REPOSITORY}.git
git remote set-url origin "https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git"
# 例えば、git fetchでブランチを指定せず、リモートリポジトリから大量に
# 全てのリモートブランチを読み込んでしまった例
git fetch origin
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 11 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (11/11), 1.68 KiB | 860.00 KiB/s, done.
From https://github.com/DL-from-Scratch/test1
* [new branch] branch1 -> origin/branch1
* [new branch] master -> origin/master
# 確認
!git branch -a -v
* master aff0f65 test1
remotes/origin/branch1 afbab7d test3
remotes/origin/master 311e71e test2
リモートブランチ、remotes/origin/branch1、remotes/origin/masterが読み込まれた状態。
リモートブランチの履歴をローカルリポジトリから削除
リモートブランチ、remotes/origin/branch1、remotes/origin/masterを削除するために、
↓ ここで、originへの接続を一旦消す。
# originへの接続を消す
!git remote remove origin
# 確認 (origin/branch1が消える)
!git branch -a -v
* master aff0f65 test1
originへの接続を消すと、リモートブランチ、remotes/origin/branch1、remotes/origin/masterの表示が消える。
↓ ここで、(ブランチから辿れなくなった)不要な履歴を削除
# ここで、不要な履歴を削除
!git reflog expire --expire=now --all
!git gc --aggressive --prune=now
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 2 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (16/16), done.
Total 16 (delta 1), reused 15 (delta 0)
# アクセス不可の確認
!git show --stat --oneline afbab7d
fatal: ambiguous argument 'afbab7d': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
# アクセス不可の確認
!git show --stat --oneline 311e71e
fatal: ambiguous argument '311e71e': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
不要なデータが削除され、コミットSHA値を用いてもアクセスが不可能になっている。(履歴から完全に削除された状態)
読み込みたいリモートブランチだけを再度読み込み直す
%%bash
# 既存内容のあるリモートリポジトリ(GitHub)を登録
git remote add origin "https://github.com/DL-from-Scratch/test1"
# https://{USER}:{ACCESS_TOKEN}@github.com/{REPOSITORY}.git
git remote set-url origin "https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git"
# 確認
git remote -v
origin https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git (fetch)
origin https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git (push)
↓ ここで、リモートリポジトリから読み込みたいブランチだけを、必要な履歴長さ(例=2個)に限定して、再度fetchする。
# リモートリポジトリから読み込みたいブランチだけを再度fetch
!git fetch --depth=2 origin master
remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
From https://github.com/DL-from-Scratch/test1
* branch master -> FETCH_HEAD
# 確認
!git branch -a -v
!git log --oneline --graph --all
* master aff0f65 test1
remotes/origin/master 311e71e test2
* 311e71e (origin/master) test2
* aff0f65 (grafted, HEAD -> master) test1
リモートブランチremotes/origin/masterのみが、直近履歴2個のみ(grafted表示)読み込まれた必要最低限の状態。(remotes/origin/branch1はローカルリポジトリから削除された状態)
試行したファイル
環境
Google Colaboratory、git version 2.25.1、access tokenを用いてGitHubへアクセス