🐵

GitHubの初期設定をやってみた

2023/01/28に公開

今年はZennで記事を沢山書こうと思っていた所、ZennとGitHubを連携したら便利そうだなとこの記事を読んでみて思いました。
ただ、今回はとりあえず、GitHubの初期設定をやってみます。

1. アカウント作成

GitHubの公式サイトの右上のSign upから登録を行います。

Sign upをクリックすると、メールアドレス、パスワード作成、ユーザー名の入力欄が出てくるのでそれぞれ記入します。

全部入力すると、パズルが出てくるのでパズルをします。
パズルに正解すると、認証コードがメールアドレスに送付されるのでそのコードを入力します。
問題なかったら、下記の画面が出てきて、アカウント作成は完了です。

2. リポジトリの作成

右上のプラスボタンから新規にリポジトリを作成する事が出来ます。

クリック後、Create a new repositoryの画面が表示されます。
そこでRepository nameなど記入します。
README fileとはそのプロジェクトの説明を書く事ができる説明書みたいなものです。後で追加する事も出来ます。
.gitignoreファイルはGitHubのレポジトリで管理したくないファイル名を除外するためのファイルです。

一通り記入したら、create repositoryボタンをクリックします。そうすると作成された画面が出てきます。

3. 基本的なコマンド

それでは作成したリポジトリで簡単なコマンドを使ってみます。
作成したリポジトリをリモート(GitHub上)からローカルのPCにコピーします。下記のコマンドをMacの場合はターミナル、Windowsの場合はコマンドプロントから実行します。
まずはローカルのどのディレクトリーにコピーするか決めます。テストなので/Users/ユーザー名/配下でも良いと思います。
それから右上のcodeの所をクリックするとリポジトリのURLがコピーできるのでコピーします。

クローンの作成

下記のコマンドを実行します。

% git clone [リポジトリのURL]

下記のようなコメントが表示されます。
※これはオープンリポジトリなので私のURLを記載しています。

% git clone https://github.com/atsushi-ambo/test-github
Cloning into 'test-github'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

リポジトリの初期化

下記のコマンドで新しいリポジトリを初期化出来ます。

% git init
Reinitialized existing Git repository in /Users/ユーザー名/test-github/.git/

ローカルリポジトリに新規ファイル追加

下記のコマンドでローカルのリポジトリに新規のファイルを追加出来ます。

git add [ファイル名]

その前にテストファイルを作成してみます。
下記のコマンドでテストファイルを作ります。
コマンドはコピーしたローカルのリポジトリに移動してから実行します。そうすると、テストファイルが作成されているのが確認出来ます。

% echo “Hello World” > GitHubtest.txt
% ls
GitHubtest.txt  README.md

このテストファイルをaddコマンドで追加します。

% git add GitHubtest.txt

追加出来ました。

ローカルリポジトリに変更の保存

addで追加したらその変更をcommitコマンドでローカルリポジトリに保存します。正確にいうとaddでインデックスに追加して、その後commitでローカルリポジトリに追加している感じです。インデックスを一回通すのはローカルリポジトリに追加するファイルを選別するためです。
下記のコマンドでcommitします。

git commit -m "[コミットメッセージ]"

私の環境では下記のメッセージを入れてcommitしてみました。

% git commit -m "テストファイル追加"
[main aa3f19e] テストファイル追加
 Committer: Atsushi Ambo <ユーザー名-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 GitHubtest.txt

リモートリポジトリ(GitHub上)に変更を反映

ローカルリポジトリに追加出来たので、今後はリモートリポジトリに追加します。
下記のコマンドを使用します。
ブランチはデフォルトのmainしか作成していないので、mainとなります。他のブランチを作成した場合のそのブランチ名を入れます。

git push origin [ブランチ名]

ただ、エラーが出てしまいました。

% git push origin main          
Missing or invalid credentials.
Error: connect ENOENT /var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'connect',
  address: '/var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock'
}
Missing or invalid credentials.
Error: connect ENOENT /var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'connect',
  address: '/var/folders/vr/n1j1fkxd25b60vlpyg3_8t500000gn/T/vscode-git-fc8d2dcd4c.sock'
}
remote: No anonymous write access.
fatal: Authentication failed for 'https://github.com/atsushi-ambo/test-github/'

調べているとアクセストークンを設定していないので権利がないためpush出来ないみたいです。

Personal access tokensの設定

そのためトークンを設定します。
Settingをクリックして下の方のDeveloper settingsをクリックします。

Personal access tokensのFine-grained tokensを選択してクリックします。
※classicよりGitHubさん的にこちらの方がおすすめみたいです。

Token name等必要な箇所を入力します。
今回はpushもしたいので、Only select repositoriesで今回作成したリポジトリを選択します。

選択できたらcreateボタンをクリックします。
完成したtokenをコピーします。今回しか表示されないのでローカルにメモしておきます。

先ほど取得したトークンを設定します。
まずはGitHub CLIをインストールします。
私が使用しているのはMacなのでWindowsの人はこちらを参照して下さい。

% brew install gh
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 2 taps (hashicorp/tap and homebrew/core).
==> New Formulae
ancient                      clang-build-analyzer         hashicorp/tap/copywrite      m1ddc                        mdless                       xcdiff                       zsh-autopair

You have 9 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Fetching gh
==> Downloading https://ghcr.io/v2/homebrew/core/gh/manifests/2.22.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/gh/blobs/sha256:e704f932cbc7b237e9143e9db3a9a32f0acd753e03d302299b69d660d3875011
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:e704f932cbc7b237e9143e9db3a9a32f0acd753e03d302299b69d660d3875011?se=2023-01-28T10%3A25%3A00Z&sig=u57FLpso8S3mj2ErguwRz
######################################################################## 100.0%
==> Pouring gh--2.22.1.arm64_ventura.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/gh/2.22.1: 153 files, 40.3MB
==> Running `brew cleanup gh`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

次に下記のコマンドを実行します。
そうすると色々聞かれるので回答していきます。先ほど取得したトークンも貼り付ける時がありますので、貼り付けます。

% gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser
...
✓ Authentication complete.
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as atsushi-ambo

上記のように表示されたら設定は完成です。
ようやく先ほどのpushが出来ます。

% git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/atsushi-ambo/test-github
   e30a2c6..7253ffa  main -> main

これでpushが完了です。
リモートリポジトリに反映されたかGitHub上で確認してみます。
テストファイルが追加されていました。

今回のGitHubの初期設定はここまでです。
他にもgit branchやgit checkoutなどのコマンドがありますが、また今度使ってみたいと思います。

Discussion