LoginSignup
0
0

More than 1 year has passed since last update.

[Git] アクセス情報をURLに埋め込んでGitHubにアクセス (Passwordの入力は不要)

Last updated at Posted at 2023-03-26

はじめに

Gitの環境を導入してGitHubにpushするまでの手順は、一通り終わっている状態から、始めます。

一からGitの環境を導入してGitHubにpushするまでの手順は、下記参照:

アクセス情報をURLに埋め込んでGitHubにアクセス

下記の方法で、「git remote set-url」を用いて、URLに「https://{USER}:{ACCESS_TOKEN}@github.com/{REPOSITORY}.git」を登録しても、GitHubにアクセス可能。

Passwordの入力は不要になる。

:: リモートリポジトリ(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"

URLの登録状態を確認すると、

:: 確認
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)

fetchでリモートリポジトリのmasterブランチの履歴情報を取得して、checkoutしてみると、

:: リモートリポジトリのmasterブランチの履歴情報を取得
git fetch origin master
:: リモートリポジトリのmasterブランチをcheckout
git checkout origin/master
出力
From https://github.com/DL-from-Scratch/test1
 * branch            master     -> FETCH_HEAD

HEAD is now at e2607a2 test1

適当な変更を加えてコミット、変更内容をpushしてGitHubへ送信すると、

:: まずローカルリポジトリのmasterブランチに戻る
git checkout master
:: リモートリポジトリのmasterブランチの位置まで進める
git merge origin/master
:: (今の内容を無視して強制的に移動する場合は: git reset --hard origin/master)
出力
Updating c9370f8..10d6e72
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)
:: 適当な変更を加えてコミット
echo abc >> test1.txt
git add .
git commit -m "test (access token in url)"
git log --oneline -1
出力
[master 10d6e72] test (access token in url)
 1 file changed, 1 insertion(+)

10d6e72 (HEAD -> master) test (access token in url)
:: 変更内容をpushしてGitHubへ送信
git push origin master
出力
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/DL-from-Scratch/test1.git
 + 263c590...10d6e72 master -> master

環境

Windows、PortableGit-2.40.0-64-bitを使用、access tokenを用いてGitHubへアクセス

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0