Git:リモートリポジトリのクローンとプル(トークン、GitHub編)

投稿日:  更新日:

Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。

Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、トークンを使ったリモートリポジトリのクローン(コピー)とプル(履歴の取得)の方法を紹介します。

BitbucketよりGitHubの方がシンプルで使い易いかも…

※環境:Android Studio Narwhal 3 Feature Drop | 2025.1.3
    Git version is 2.50.1
※前提:リモートリポジトリの構築済み

スポンサーリンク

トークンによるアクセス

リポジトリの所有者以外のユーザーが、リポジトリへアクセスしたい場合は、所有者の発行するトークンを使うとアクセス出来ます。

トークンとは

トークンとは「ユーザーの権限の一部を委譲したチケット」です。

トークン有りトークン無し
Publicをtokenでアクセス
Privateをtokenでアクセス

◎:誰でも無条件に可(≒全世界に公開)
〇:トークンにより可
X:出来ない
Publicリポジトリ
Privateリポジトリ

◎:誰でも無条件に可(≒全世界に公開)
〇:トークンにより可
X:出来ない

このチケットを持つアプリは、所有者に成り代わってアクセスを行えます。

スポンサーリンク

アクセスの様子

トークンはGitHubのユーザーが発行します。

ですので、ユーザー(UserA)が所持するリポジトリのみに有効です。他者(UserB)が発行したトークンは無効であり、アクセスは許されません。

トークンを使ったアクセスンの様子

トークンは、「1:1(トークン:ツール)」や「1:多」といった配布が出来ます。

「1:多」の配布は、トークンの有効期限が決め難かったり(同じ期限になってしまう)、配布先毎にトークンを廃止できなかったり、許可範囲の制限が緩くなるのでお勧めしません。

手間がかかっても、配布先毎にトークンを発行する方法が良いです。

また、トークンの注意点は「所有者に成り代わってアクセスする」点です。トークンはユーザーに関連付けられています。

委譲される権限の内容次第で、ユーザーが所有する全てのリポジトリへアクセス出来る場合があります。

スポンサーリンク

2種類のトークン

GitHubは、表のような2種類のトークンが発行出来ます。

トークン特徴
Tokens(Classic)・発行元のユーザーはサインインユーザーになる
・有効期間が指定できる
・全てのリポジトリが対象になる
・権限の範囲指定が大雑把
Fine-grained tokens・発行元のユーザー(共有アカウントを含む)が選択できる
・有効期間が指定できる
・対象のリポジトリが指定できる、一部または全て
・権限の範囲指定が詳細

GitHubのドキュメントでは「Fine-grained tokens」の利用が推奨されています。

両者の特徴は異なりますが、利用方法は同じです。この記事は「Fine-grained tokens」を利用する方法のみを記載します。


また、Tokens(Classic)にしか出来ない項目があるようです。その場合は、Tokens(Classic)を使う必要があります。詳細はGitHubドキュメントを参照して下さい。

スポンサーリンク

トークンの発行

GitHubのサイトへサインイン後、Settingsページの「Developer Settings」を開きます。

「Generate new token」ボタンを押して、トークンの作成を開始します。

Developer Settings

トークンの情報を入力していきます。

トークンの情報入力

ここでは、対象のリポジトリを限定したトークンを作成しています。

また、許可する権限に「Contents」を指定し、アクセスに「Read and Write」を指定すると、リポジトリに対してRead/Writeが可能になります。つまり、Clone/Push/Pullが出来ます。

「Medadata」は自動的に付与される権限です。そのままで良いです。

トークンの情報入力

再確認のダイアログが表示されるので、問題なければボタンを押します。

トークン作成の再確認

最後に、作成されたトークンが表示されます。

トークンの作成結果

ここで、表示されているトークンを、どこかに保存または書き留めて下さい。この表示を閉じると、以後、トークンの再表示は出来ません。

スポンサーリンク

クローンの実施

リポジトリのクローンを行う方法は、次の2通りがあります

WelcomパネルからFileメニューから
WelcomパネルからClone
FileメニューからClone

「Clone」ボタンの押下でクローンが開始されます。

この後、トークンによるユーザー認証の手続きがあります。

スポンサーリンク

ユーザー認証

ユーザー認証(Log in)のダイアログが表示されたら、トークンの利用を選びます。

トークンを求められるので、発行済みのトークン(発行時に書き留めた物)を入力します。

トークンによるユーザー認証

スポンサーリンク

プルの実施

プルによりリモートリポジトリから最新の履歴が取得できます。

リポジトリのプルは「Git」メニューから行います。

プル(Pull)の実施

「Pull」ボタンの押下でプルが開始されます。

スポンサーリンク

リモートの定義

クローン後のAndroid Studioプロジェクトは、リモートリポジトリが定義されています。

> git config --local  --list  
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
submodule.active=.
remote.origin.url=https://github.com/ユーザ名/SampleApp.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

クローンを行った時に指定したURLが、そのまま指定されています。

スポンサーリンク

トークンの保持

発行されたトークンはAndroid Studio内に保持されるので、クローン後のアクセスは、ユーザ認証が不要です。

トークンの状況は、Android Studioの「Settings->Version Control->GitHub」で確認できます。

トークンの保持

スポンサーリンク

Applendix:トークンの許可項目

トークンを作成する際に、「Permissions」欄で許可する権限を選びます。

その際に「Learn more about permissions」を参照すると、権限の詳細を確認できるのですが、Web APIのコマンドが表示されるだけです。コマンドと権限が結び付かないので、非常に解り難いです。

ですので、権限の概要を抽出して、表にしました。参考にしてください。

RepositoriesAccount
項目概要
ActionsWorkflows, workflow runs and artifacts.
AdministrationRepository creation, deletion, settings, teams, and collaborators.
AttestationsCreate and retrieve attestations for a repository.
Code scanning alertsView and manage code scanning alerts.
CodespacesCreate, edit, delete and list Codespaces.
Codespaces lifecycle adminManage the lifecycle of Codespaces, including starting and stopping.
Codespaces metadataAccess Codespaces metadata including the devcontainers and machine type.
Codespaces secretsestrict Codespaces user secrets modifications to specific repositories.
Commit statusesCommit statuses.
ContentsRepository contents, commits, branches, downloads, releases, and merges.
Custom propertiesRead and write repository custom properties values at the repository level, when allowed by the property.
Dependabot alertsRetrieve Dependabot alerts.
Dependabot secretsManage Dependabot repository secrets.
DeploymentsDeployments and deployment statuses.
DiscussionsDiscussions and related comments and labels.
EnviromentsManage repository environments.
IssuesIssues and related comments, assignees, labels, and milestones.
Merge queuesManage a repository's merge queues
MetadataSearch repositories, list collaborators, and access repository metadata.
PagesRetrieve Pages statuses, configuration, and builds, as well as create new builds.
Pull requestsPull requests and related comments, assignees, labels, milestones, and merges.
Repository security advisoriesView and manage repository security advisories.
Secret scanning alart dismissal requestsView and manage secret scanning alert dismissal requests
Secret scanning alertsView and manage secret scanning alerts.
Secret scanning push protection bypass requestsReview and manage repository secret scanning push protection bypass requests.
SecretsManage Actions repository secrets.
VariablesManage Actions repository variables.
WebhooksManage the post-receive hooks for a repository.
WorkflowsUpdate GitHub Action workflow files.
項目概要
Block another userView and manage users blocked by the user.
Codespaces user secretsManage Codespaces user secrets.
Copilot ChatThis application will receive your GitHub ID, your GitHub Copilot Chat session messages (not including messages sent to another application), and timestamps of provided GitHub Copilot Chat session messages. This permission must be enabled for Copilot Extensions.
Copilot Editor ContextThis application will receive bits of Editor Context (e.g. currently opened file) whenever you send it a message through Copilot Chat.
Copilot RequestsSend Copilot requests.
Email addressesManage a user's email addresses.
EventsView events triggered by a user's activity.
FollowersA user's followers
GPG keysView and manage a user's GPG keys.
GistsCreate and modify a user's gists and comments.
Git SSH keysGit SSH keys
Interaction limitsInteraction limits on repositories
Knowledge basesView knowledge bases for a user.
ModelsAllows access to GitHub Models.
PlanView a user's plan.
Private repository invitationsView a user's invitations to private repositories
ProfileManage a user's profile settings.
SSH signing keysView and manage a user's SSH signing keys.
StarringList and manage repositories a user is starring.
WatchingList and change repositories a user is subscribed to.
スポンサーリンク

関連記事:

Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、ローカルリポジトリの構築とコミット(履歴の登録)の方法を紹介します。 ※環境:Android Studio Electric Eel | 2022.1.1 ※Git自体の説明は行いません。 ...
Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、リモートリポジトリの構築とアプリパスワードを使ったプッシュ(履歴の登録)の方法を紹介します。 ※環境:Android Studio Electric Eel | 2022.1.1 ※前提:ローカルリポジトリの構築済み ...
Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、SSHを使ったリポジトリのクローン(コピー)とプル(履歴の取得)の方法を紹介します。 ※この記事に掲載していた「SSHとは」は、「SSHで安全な通信経路を確立」へ分離しました。 ※環境:Android Studio Electric Eel | 2022.1.1     OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3 ※前提:リモートリポジトリの構築済み ...
Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、アプリパスワードを使ったリモートリポジトリのクローン(コピー)とプル(履歴の取得)の方法を紹介します。 ※環境:Android Studio Electric Eel | 2022.1.1 ※前提:リモートリポジトリの構築済み ...
Android Studioで採用されている、パスワード管理ツール「KeePass」について、まとめます。 ※環境:Android Studio Narwhal 3 Feature Drop | 2025.1.3 ...
サーバーとクライアント間でデータを受け渡しする際に、安全な通信経路を確立する目的で、SSHが広く使われています。 Android Studio(Gitクライアント)とBitbucketまたはGitHub(Gitサーバー)間も、SSHが利用できます。 今回は「SSHの概要」を、まとめます。 ※この記事は「Git:リポジトリのクローンとプル(SSH、Bitbucket編)」から「SSHとは」を分離したものです。加筆も行っています。 ...
Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、リモートリポジトリの構築とGitHubアカウントを使ったプッシュ(履歴の登録)の方法を紹介します。 BitbucketよりGitHubの方がシンプルで使い易いかも... ※環境:Android Studio Narwhal 3 Feature Drop | 2025.1.3     Git version is 2.50.1 ※前提:ローカルリポジトリの構築済み ...
Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。 Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、SSHを使ったリポジトリのクローン(コピー)とプル(履歴の取得)の方法を紹介します。 BitbucketよりGitHubの方がシンプルで使い易いかも… ※環境:Android Studio Narwhal 3 Feature Drop | 2025.1.3     Git version is 2.50.1     OpenSSH_for_Windows_9.5p2, LibreSSL 3.8.2     Windows 11 Pro 24H2 ※前提:リモートリポジトリの構築済み ...
スポンサーリンク