LoginSignup
0
1

More than 1 year has passed since last update.

[Git] 一からGitの環境を導入してGitHubにpushするまでの手順 (23年03月, v2.40.0版, Windows上)

Last updated at Posted at 2023-03-25

はじめに

インストールから始めて、GitHubにpushするまでの手順を記載。

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

Git for Windowsをダウンロード

Git for WindowsのダウンロードWebページ:

image.png

下記のGit for Windowsをダウンロード:

PortableGit-2.40.0-64-bit.7z.exe
https://github.com/git-for-windows/git/releases/download/v2.40.0.windows.1/PortableGit-2.40.0-64-bit.7z.exe
image.png

PortableGit-2.40.0-64-bit.7z.exeを展開して、(約361MB)
中にある、git-bash.exe、または、git-cmd.exeを主に利用。
image.png

ローカルリポジトリ作成

例えば、git-cmd.exeを起動して、コンソールに下記のコマンドを入力して実行。

:: Gitの最低限必要な初期設定
git config --global user.email "test1@example.com"
git config --global user.name "Test1 Name"

:: テスト用フォルダを作成
cd /
mkdir "test-space"
cd "test-space"
mkdir "local-repo1"
cd "local-repo1"

:: ローカルのリポジトリを作成
git init

:: テスト用に変更を加えてコミット
echo test1 > test1.txt
git add .
git commit -m "test message"

GitHubのaccess tokenを用意

GitHubへのアクセスのために、access tokenを用意。

GitHubのWebページ→アカウントでログイン後→Settings→Developer settings→Personal access tokens (classic)→Generate new token (classic)→☑repoにチェック→Generate tokenボタンを押下して作成。

https://github.com/settings/tokens
image.png
image.png
image.png

access token: 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を取得。

GitHubで新しいリポジトリを作成

GitHubのRepositoriesタブのページで、Newボタンを押下、

https://github.com/DL-from-Scratch?tab=repositories
image.png

リポジトリ名を入力して作成。
image.png
image.png

GitHubへpushして内容を送信

前述のローカルリポジトリの内容をGitHubへ送信。git-cmd.exeで続けて、コンソールに下記のコマンドを入力して実行。

:: テスト用リモートリポジトリ(GitHub)を登録
git remote add origin "https://github.com/DL-from-Scratch/test1"

:: リモートリポジトリへ送信
git push origin master

「no helper」を選択、
image.png

ユーザ名(アカウント名)と、Passwordにaccess tokenを入力して、アクセス可能。

例:
Username: DL-from-Scratch
Password: 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

D:\test-space\local-repo1> git push origin master
Username for 'https://github.com': DL-from-Scratch
Password for 'https://DL-from-Scratch@github.com':

GitHubへ送信され、Webページに反映される。

https://github.com/DL-from-Scratch/test1
image.png

GitHubの既存内容のあるリモートリポジトリからCheckoutして内容を取得する

ここで、GitHubにある別のリポジトリtest2から、checkoutして内容を取得するには、

:: テスト用リモートリポジトリ(GitHub)を登録
git remote add origin2 "https://github.com/DL-from-Scratch/test2"

:: 情報を取得
git fetch origin2 master
出力
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), 959 bytes | 2.00 KiB/s, done.
From https://github.com/DL-from-Scratch/test2
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin2/master
:: 確認
git remote -v
git branch -a -v
git log --oneline --graph --all --stat
出力
origin  https://github.com/DL-from-Scratch/test1 (fetch)
origin  https://github.com/DL-from-Scratch/test1 (push)
origin2 https://github.com/DL-from-Scratch/test2 (fetch)
origin2 https://github.com/DL-from-Scratch/test2 (push)

* master                 e2607a2 test1
  remotes/origin/master  e2607a2 test1
  remotes/origin2/master c10e49d Add files via upload

* c10e49d (origin2/master) Add files via upload
|  subfolder11/test1.txt | 3 +++
|  subfolder11/test2.txt | 1 +
|  2 files changed, 4 insertions(+)
* c850d06 test message
   test1.txt | 1 +
   1 file changed, 1 insertion(+)
* e2607a2 (HEAD -> master, origin/master) test1
   test1.txt | 1 +
   1 file changed, 1 insertion(+)

(※同じ1つのローカルリポジトリ内にtest1、test2の2つのリポジトリが含まれる状態)

origin2/masterをcheckoutして、作業フォルダにファイル内容を配置。

:: ファイル内容をcheckout
git checkout origin2/master

:: 確認
dir /b
出力
subfolder11
test1.txt

別のリポジトリtest2から内容が取得・反映されている。

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

下記の方法で、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)

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

:: 適当な変更を加えてコミット
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へアクセス

PC環境によってはgit-bash.exeがエラーで起動せず

Git for Windowsバージョン2.39.2では、git-bash.exeがエラーで起動せず、git-cmd.exeでもviエディタが起動しない。

バージョン2.40.0では、問題なく動作。

0
1
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
1