コンテンツへスキップ

GitHub 使い方メモ

自分の為のメモです。いろいろと書き足していく予定です。

初めに

Git の入手先

クラウドのイメージ強いですが、ローカルにクライアントをインストールして使います。
CLI のイメージ強いですが、結構GUI も好きです。

トップページ:https://git-scm.com

インスト―ルのページ:https://git-scm.com/downloads

「Click here to download」からGit をダウンロードしてインストール。

図1: git-scm.com の「Install」ページ(Windows向け)。「Click here to download」のリンクからGit for Windowsをダウンロードできる
図1: git-scm.com の「Install」ページ(Windows向け)。「Click here to download」のリンクからGit for Windowsをダウンロードできる

インストール

インスト―ル画面のキャプチャ。こんな感じでインスト―ルしました。
ほぼ、デフォルトです。

インストール画面のキャプチャ
図2: Git 2.45.1 Setupの「Select Destination Location」画面。インストール先が「C:\Program Files\Git」に設定されている
図2: Git 2.45.1 Setupの「Select Destination Location」画面。インストール先が「C:\Program Files\Git」に設定されている
図3: Git 2.45.1 Setupの「Select Components」画面。インストールするコンポーネントの選択で、デフォルトの項目にチェックが入っている
図3: Git 2.45.1 Setupの「Select Components」画面。インストールするコンポーネントの選択で、デフォルトの項目にチェックが入っている
図4: Git 2.45.1 Setupの「Select Start Menu Folder」画面。スタートメニューフォルダーが「Git」に設定されている
図4: Git 2.45.1 Setupの「Select Start Menu Folder」画面。スタートメニューフォルダーが「Git」に設定されている
図5: Git 2.45.1 Setupの「Choosing the default editor used by Git」画面。「Use Notepad++ as Git's default editor」が選択されている
図5: Git 2.45.1 Setupの「Choosing the default editor used by Git」画面。「Use Notepad++ as Git’s default editor」が選択されている
図6: Git 2.45.1 Setupの「Adjusting the name of the initial branch in new repositories」画面。「Let Git decide」が選択されている
図6: Git 2.45.1 Setupの「Adjusting the name of the initial branch in new repositories」画面。「Let Git decide」が選択されている
図7: Git 2.45.1 Setupの「Adjusting your PATH environment」画面。「Git from the command line and also from 3rd-party software」(推奨)が選択されている
図7: Git 2.45.1 Setupの「Adjusting your PATH environment」画面。「Git from the command line and also from 3rd-party software」(推奨)が選択されている
図8: Git 2.45.1 Setupの「Choosing the SSH executable」画面。「Use bundled OpenSSH」が選択されている
図8: Git 2.45.1 Setupの「Choosing the SSH executable」画面。「Use bundled OpenSSH」が選択されている
図9: Git 2.45.1 Setupの「Choosing HTTPS transport backend」画面。「Use the OpenSSL library」が選択されている
図9: Git 2.45.1 Setupの「Choosing HTTPS transport backend」画面。「Use the OpenSSL library」が選択されている
図10: Git 2.45.1 Setupの「Configuring the line ending conversions」画面。「Checkout Windows-style, commit Unix-style line endings」が選択されている
図10: Git 2.45.1 Setupの「Configuring the line ending conversions」画面。「Checkout Windows-style, commit Unix-style line endings」が選択されている
図11: Git 2.45.1 Setupの「Configuring the terminal emulator to use with Git Bash」画面。「Use MinTTY」が選択されている
図11: Git 2.45.1 Setupの「Configuring the terminal emulator to use with Git Bash」画面。「Use MinTTY」が選択されている
図12: Git 2.45.1 Setupの「Choose the default behavior of git pull」画面。「Fast-forward or merge」が選択されている
図12: Git 2.45.1 Setupの「Choose the default behavior of git pull」画面。「Fast-forward or merge」が選択されている
図13: Git 2.45.1 Setupの「Choose a credential helper」画面。「Git Credential Manager」が選択されている
図13: Git 2.45.1 Setupの「Choose a credential helper」画面。「Git Credential Manager」が選択されている
図14: Git 2.45.1 Setupの「Configuring extra options」画面。「Enable file system caching」にチェックが入っている
図14: Git 2.45.1 Setupの「Configuring extra options」画面。「Enable file system caching」にチェックが入っている
図15: Git 2.45.1 Setupの「Configuring experimental options」画面。「Enable experimental built-in file system monitor」にチェックが入っている
図15: Git 2.45.1 Setupの「Configuring experimental options」画面。「Enable experimental built-in file system monitor」にチェックが入っている
図16: Git 2.45.1 Setupの「Completing the Git Setup Wizard」画面。インストールが完了し、「Finish」ボタンが表示されている
図16: Git 2.45.1 Setupの「Completing the Git Setup Wizard」画面。インストールが完了し、「Finish」ボタンが表示されている

使い始める

1.5 使い始める – 初期設定

図17: エクスプローラーで空の「gpost」フォルダーを右クリックした際のコンテキストメニュー。「Open Git GUI here」「Open Git Bash here」の項目が表示されている
図17: エクスプローラーで空の「gpost」フォルダーを右クリックした際のコンテキストメニュー。「Open Git GUI here」「Open Git Bash here」の項目が表示されている

ユーザー名とメールアドレスの設定

 git config --global user.name unimoni
 git config --global user.email unimonii@test.dummy.com

確認

git config --list

一番下に追加されている。

図18: Git Bashで「git config --list」を実行した結果。一覧の一番下に「user.name=unimoni」「user.mail=...」が追加されている
図18: Git Bashで「git config –list」を実行した結果。一覧の一番下に「user.name=unimoni」「user.mail=…」が追加されている

git の削除(Windows 版)

Linix のコマンドをWindows でやるならこんな感じですかね ^^

rmdir /S /Q .

リポジトリの作成

作業フォルダの初期化(新規作成)

 git init
git --help の出力(45行)
$ git --help
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--config-env=<name>=<envvar>] <command> [<args>]

These are common Git commands used in various situations:
魏t
start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

クローンする

git clone https://github.com/*********/gpost.git ./test

サインインを聞かれるので、ブラウザでサインインを選択して、

図19: 「Connect to GitHub」のサインイン画面。「Browser/Device」タブが選択され、「Sign in with your browser」ボタンが表示されている
図19: 「Connect to GitHub」のサインイン画面。「Browser/Device」タブが選択され、「Sign in with your browser」ボタンが表示されている

ユーザーアカウントと、パスワードでサインインする。

図20: GitHubの「Sign in」ページ。ユーザー名/メールアドレスとパスワードを入力する欄があり、「Git Credential Manager」との連携のためのサインイン画面になっている
図20: GitHubの「Sign in」ページ。ユーザー名/メールアドレスとパスワードを入力する欄があり、「Git Credential Manager」との連携のためのサインイン画面になっている

図21: Git Bashのターミナル画面。「git clone https://github.com/.../gpost.git ./test」を実行し、リポジトリのクローンが進行中の様子
図21: Git Bashのターミナル画面。「git clone https://github.com/…/gpost.git ./test」を実行し、リポジトリのクローンが進行中の様子

ブランチ

ブランチって枝の事で、派生のことだと思ってます。

ブランチ切り替え

既存ブランチの変更
 git checkout test1

新規ブランチへの変更
 git switch -c root1

ローカルに作成
git branch <branch-name>

ローカルに別のブランチのとしてクローン
git checkout -b [ローカルで作るブランチ名] [リモートリポジトリ名]/[元となるリモートブランチ名]

アップロード
git push -u origin <branch-name>

ブランチの削除

git push origin --delete

ソースの管理

流れとしては、コミット →プッシュ →マージ

https://qiita.com/yukiyoshimura/items/7aa4a8f8db493ab97c2b

管理の対象を追加する。
コミットの対象を追加するという解釈でも。
「.」はフォルダ全体をさす。

 git add .

逆の操作(管理の対象から除外する。)は「rm」

ステータス

管理対象の状況を確認する。新規で追加したとか、編集されている、とか

git status

前回のコミットからの変更点を確認する。

git diff

コミット

ローカルにある編集内容を確定する感じ。「-m」でコメントを入れる。
プッシュ(アップロード)する前の準備みたいな。

 git commit -m"comment"

プッシュ

git push origin ブランチ名

origin って何?

git push origin の origin は、リモートリポジトリのデフォルトの名前です。Gitでは、ローカルリポジトリとリモートリポジトリを使いますが、origin はリモートリポジトリの場所(URL)を簡単に呼ぶための別名です。

プッシュ前
図22: GitHubのリポジトリ「gpost」のブランチ「test2」のページ。「8 commits ahead of, 17 commits behind main」と表示され、プッシュ前の状態を示している
図22: GitHubのリポジトリ「gpost」のブランチ「test2」のページ。「8 commits ahead of, 17 commits behind main」と表示され、プッシュ前の状態を示している
プッシュ後

変わってる。

図23: 同じGitHubリポジトリのページ。プッシュ後の状態で、「11 commits ahead of main」に変わり、新しいコミット「Robodrill-3axis testversion」と新規ファイル「spndl_cl.lst」が追加されている
図23: 同じGitHubリポジトリのページ。プッシュ後の状態で、「11 commits ahead of main」に変わり、新しいコミット「Robodrill-3axis testversion」と新規ファイル「spndl_cl.lst」が追加されている

不整合とか起きていると、書き込みを拒否(下記のエラー。)されるので、git pull したりして修正します。
その時にコメントを書き込め見たいな画面になるのですが、unix 系のvi エディタになります。(環境設定でメモ帳に出来たりするのかも?)

 ! [rejected]        main -> test2 (non-fast-forward)
error: failed to push some refs to 'https://github.com/*******/gpost.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

参考:vi エディタ

SSL の設定

プッシュでエラーが出るとき。多分、ファイルが大きいから?

✅ 1. SSHキーを作成

ターミナルで:

ssh-keygen -t ed25519 -C "あなたのGitHubメールアドレス"
図24: Git Bashのターミナル画面。「ssh-keygen -t ed25519 -C "..."」コマンドを実行し、SSH鍵ペアを生成した結果。鍵の保存先やフィンガープリント、ランダムアート画像が表示されている
図24: Git Bashのターミナル画面。「ssh-keygen -t ed25519 -C “…”」コマンドを実行し、SSH鍵ペアを生成した結果。鍵の保存先やフィンガープリント、ランダムアート画像が表示されている
  • Enterキーでデフォルト保存。
  • パスフレーズは任意。

✅ 2. SSHエージェントに追加

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
図25: Git Bashのターミナル画面。「eval "$(ssh-agent -s)"」と「ssh-add ~/.ssh/id_ed25519」を実行し、SSHエージェントに秘密鍵が追加された結果が表示されている
図25: Git Bashのターミナル画面。「eval “$(ssh-agent -s)”」と「ssh-add ~/.ssh/id_ed25519」を実行し、SSHエージェントに秘密鍵が追加された結果が表示されている

✅ 3. 公開鍵をGitHubに登録

  • 公開鍵を表示:
cat ~/.ssh/id_ed25519.pub
  • 表示されたキーをコピー。
  • GitHub → Settings → SSH and GPG keys → New SSH key → 貼り付け。

✅ SSHキー登録の最新手順
図26: GitHubの「SSH keys」設定ページ。登録された認証キー「DESKTOP-0OIFL0J vm test7」が一覧に表示されている
図26: GitHubの「SSH keys」設定ページ。登録された認証キー「DESKTOP-0OIFL0J vm test7」が一覧に表示されている

GitHubにログイン

右上のプロフィールアイコン → Settings

左メニューで 「Access」 → 「SSH and GPG keys」 を探す
(「Access」セクションの中にあります)

New SSH key をクリック

  • Title:わかりやすい名前(例:My Laptop)
  • Key:cat ~/.ssh/id_ed25519.pub で表示した公開鍵を貼り付け

Add SSH key を押す

✅ 4. 接続確認

ssh -T git@github.com
図27: Git Bashのターミナル画面。「ssh -T git@github.com」を実行し、「Hi seiichi-303! You've successfully authenticated, but GitHub does not provide shell access.」というSSH接続確認の成功メッセージが表示されている
図27: Git Bashのターミナル画面。「ssh -T git@github.com」を実行し、「Hi seiichi-303! You’ve successfully authenticated, but GitHub does not provide shell access.」というSSH接続確認の成功メッセージが表示されている

→ 「Hi ユーザー名! You’ve successfully authenticated…」が出ればOK。

✅ 5. 再度プッシュ

git push origin test3
図28: Git GUIの「push origin」ダイアログ。SSH経由で「github.com:seiichi-303/gpost.git」へのプッシュが成功し、ステータスバーに「Success」と表示されている
図28: Git GUIの「push origin」ダイアログ。SSH経由で「github.com:seiichi-303/gpost.git」へのプッシュが成功し、ステータスバーに「Success」と表示されている

(Visited 122 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です