Skip to main content
ドキュメントには� �繁に更新が� えられ、その都度公開されています。本ページの翻訳はま� 未完成な部分があることをご了承く� さい。最新の情� �については、英語のドキュメンテーションをご参照く� さい。本ページの翻訳に問題がある� �合はこちらまでご連絡く� さい。

このバージョンの GitHub Enterprise はこの日付をもって終了となりました: 2022-06-03. 重大なセキュリティの問題に対してであっても、パッチリリースは作成されません。 パフォーマンスの向上、セキュリティの改善、新機能のためには、最新バージョンのGitHub Enterpriseにアップグレードしてく� さい。 アップグレードに関する支援については、GitHub Enterprise supportに連絡してく� さい。

機密データをリポジトリから削除する

Git リポジトリへのパスワードや SSH キーといった機密データをコミットする� �合、そのデータを履歴から削除することができます。 To entirely remove unwanted files from a repository's history you can use either the git filter-repo tool or the BFG Repo-Cleaner open source tool.

The git filter-repo tool and the BFG Repo-Cleaner rewrite your repository's history, which changes the SHAs for existing commits that you alter and any dependent commits. コミットの SHA が変更されると、リポジトリでオープンされたプルリクエストに影響する可能性があります。 ファイルをリポジトリから削除する前に、オープンプルリクエストをすべてマージまたはクローズすることを推奨します。

git rm によって、最新のコミットからファイルを削除することができます。 For information on removing a file that was added with the latest commit, see "About large files on GitHub."

Warning: This article tells you how to make commits with sensitive data unreachable from any branches or tags in your repository on GitHub Enterprise Serverインスタンス. However, those commits may still be accessible in any clones or forks of your repository, directly via their SHA-1 hashes in cached views on GitHub Enterprise Server, and through any pull requests that reference them. You cannot remove sensitive data from other users' clones or forks of your repository, but you can permanently remove cached views and references to the sensitive data in pull requests on GitHub Enterprise Server by contacting your site administrator.

Once you have pushed a commit to GitHub Enterprise Server, you should consider any sensitive data in the commit compromised. If you committed a password, change it! キーをコミットした� �合は、新たに生成してく� さい。 Removing the compromised data doesn't resolve its initial exposure, especially in existing clones or forks of your repository. Consider these limitations in your decision to rewrite your repository's history.

ファイルをリポジトリの履歴からパージする

You can purge a file from your repository's history using either the git filter-repo tool or the BFG Repo-Cleaner open source tool.

BFG を使用する

BFG Repo-Cleaner は、オープンソースコミュニティによって構築およびメンテナンスされているツールです。 これは、不要なデータを削除する手段として、git filter-branch より高速でシンプルです。

たとえば、機密データを含むファイルを削除して、最新のコミットをそのままにしておくには、次を実行します:

$ bfg --delete-files 機密データを含むファイル

passwords.txt にリストされているすべてのテキストについて、リポジトリの履歴にあれば置き換えるには、次を実行します:

$ bfg --replace-text passwords.txt

機密データが削除されたら、変更を GitHub Enterprise Server に強制的にプッシュする必要があります。 Force pushing rewrites the repository history, which removes sensitive data from the commit history. If you force push, it may overwrite commits that other people have based their work on.

$ git push --force

完全な使用方法とダウンロード手� �については、BFG Repo-Cleaner のドキュメントを参照してく� さい。

Using git filter-repo

Warning: If you run git filter-repo after stashing changes, you won't be able to retrieve your changes with other stash commands. Before running git filter-repo, we recommend unstashing any changes you've made. stash した最後の一連の変更を unstash するには、git stash show -p | git apply -R を実行します。 For more information, see Git Tools - Stashing and Cleaning.

To illustrate how git filter-repo works, we'll show you how to remove your file with sensitive data from the history of your repository and add it to .gitignore to ensure that it is not accidentally re-committed.

  1. Install the latest release of the git filter-repo tool. You can install git-filter-repo manually or by using a package manager. For example, to install the tool with HomeBrew, use the brew install command.

    brew install git-filter-repo
    

    For more information, see INSTALL.md in the newren/git-filter-repo repository.

  2. 機密データを含むリポジトリのローカルコピーが履歴にま� ない� �合は、ローカルコンピュータにリポジトリのクローンを作成します。

    $ git clone https://hostname/ユーザ名/リポジトリ
    > Initialized empty Git repository in /Users/ファイルパス/リポジトリ/.git/
    > remote: Counting objects: 1301, done.
    > remote: Compressing objects: 100% (769/769), done.
    > remote: Total 1301 (delta 724), reused 910 (delta 522)
    > Receiving objects: 100% (1301/1301), 164.39 KiB, done.
    > Resolving deltas: 100% (724/724), done.
  3. リポジトリのワーキングディレクトリに移動します。

    $ cd リポジトリ
  4. 次のコマンドを実行します。機密データを含むファイルへのパスは、ファイル名� けではなく、削除するファイルへのパスで置き換えます。 その引数により、次のことが行われます:

    • 各ブランチとタグの履歴全体を強制的に Git で処理するが、チェックアウトはしない
    • 指定のファイルを削除することにより、生成された空のコミットも削除される
    • Remove some configurations, such as the remote URL, stored in the .git/config file. You may want to back up this file in advance for restoration later.
    • 既存のタグを上書きする
      $ git filter-repo --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
        Parsed 197 commits
        New history written in 0.11 seconds; now repacking/cleaning...
        Repacking your repo and cleaning out old unneeded objects
        Enumerating objects: 210, done.
        Counting objects: 100% (210/210), done.
        Delta compression using up to 12 threads
        Compressing objects: 100% (127/127), done.
        Writing objects: 100% (210/210), done.
        Building bitmaps: 100% (48/48), done.
        Total 210 (delta 98), reused 144 (delta 75), pack-reused 0
        Completely finished after 0.64 seconds.

    メモ: 機密データを含む当該ファイルが (移動されたか名前が変更されたため) 他のパスに存在していた� �合、このコマンドはそのパスでも実行する必要があります。

  5. 機密データを含むファイルを、誤って再度コミットしないようにするため、.gitignore に追� します。

    $ echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
    $ git add .gitignore
    $ git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
    > [main 051452f] Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore
    >  1 files changed, 1 insertions(+), 0 deletions(-)
  6. リポジトリの履歴から削除対象をすべて削除したこと、すべてのブランチがチェックアウトされたことをダブルチェックします。

  7. Once you're happy with the state of your repository, force-push your local changes to overwrite your repository on GitHub Enterprise Serverインスタンス, as well as all the branches you've pushed up. A force push is required to remove sensitive data from your commit history.

    $ git push origin --force --all
    > Counting objects: 1074, done.
    > Delta compression using 2 threads.
    > Compressing objects: 100% (677/677), done.
    > Writing objects: 100% (1058/1058), 148.85 KiB, done.
    > Total 1058 (delta 590), reused 602 (delta 378)
    > To https://hostname/YOUR-USERNAME/YOUR-REPOSITORY.git
    >  + 48dc599...051452f main -> main (forced update)
  8. 機密データをタグ付きリリースから削除するため、Git タグに対しても次のようにフォースプッシュする必要があります。

    $ git push origin --force --tags
    > Counting objects: 321, done.
    > Delta compression using up to 8 threads.
    > Compressing objects: 100% (166/166), done.
    > Writing objects: 100% (321/321), 331.74 KiB | 0 bytes/s, done.
    > Total 321 (delta 124), reused 269 (delta 108)
    > To https://hostname/YOUR-USERNAME/YOUR-REPOSITORY.git
    >  + 48dc599...051452f main -> main (forced update)

Fully removing the data from GitHub

After using either the BFG tool or git filter-repo to remove the sensitive data and pushing your changes to GitHub Enterprise Server, you must take a few more steps to fully remove the data from GitHub Enterprise Server.

  1. your site administrator に連絡し、GitHub Enterprise Server 上で、キャッシュされているビューと、プルリクエストでの機密データへの参照を削除するよう依� �します。 Please provide the name of the repository and/or a link to the commit you need removed. For more information about how site administrators can remove unreachable Git objects, see "Command line utilities."

  2. コラボレータには、 作成したブランチを古い (汚染された) リポジトリ履歴からリベースする (マージしない) よう伝えます。 マージコミットを 1 回でも行うと、パージで問題が発生したばかりの汚染された履歴の一部または全部が再導入されてしまいます。

  3. After some time has passed and you're confident that the BFG tool / git filter-repo had no unintended side effects, you can force all objects in your local repository to be dereferenced and garbage collected with the following commands (using Git 1.8.5 or newer):

    $ git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
    $ git reflog expire --expire=now --all
    $ git gc --prune=now
    > Counting objects: 2437, done.
    > Delta compression using up to 4 threads.
    > Compressing objects: 100% (1378/1378), done.
    > Writing objects: 100% (2437/2437), done.
    > Total 2437 (delta 1461), reused 1802 (delta 1048)

    注釈: フィルタした履歴を、新規または空のリポジトリにプッシュして、GitHub Enterprise Server から新しいクローンを作成することによっても、同じことができます。

将来にわたって誤ったコミットを回避する

コミット対象でないものがコミットされるのを回避するためのシンプルな方法がいくつかあります。

  • GitHub Desktopgitk のようなビジュアルプログラ� を使用して、変更をコミットします。 ビジュアルプログラ� は通常、各コミットでどのファイルが追� 、削除、変更されるかを正確に把握しやすくするものです。
  • コマンドラインでは catch-all コマンド、git add . および git commit -a は使用しないようにします。ファイルを個別にステージングするには、代わりに git add filename および git rm filename を使用します。
  • 各ファイル内の変更を個別にレビューしステージングするには、git add --interactive を使用します。
  • コミットのためにステージングされている変更をレビューするには、git diff --cached を使用します。 これはまさに、git commit-a フラグを使用しない限りにおいて生成される diff です。

参考リンク