0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカル環境でのGit初期設定ガイド

Last updated at Posted at 2025-03-24

はじめに

MacOSでGit初期設定をする方法についてのメモとなります。
(何回やっても忘れてるので...😭)

環境

■ MacOS
14.0
■ チップ
Apple M2

1.Homebrewをインストール

HomebrewはmacOS上で動作するパッケージ管理ツールのひとつとなります。
Homebrewを用いて様々なパッケージをインストールすることができます。

■ Homebrew
https://brew.sh

以下のコマンドでインストールできます。
コマンドが変わる可能性があるので、ホームページで確認するのをお勧めします!

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Homebrewをインストールしたものの、まだHomebrewが使えないようです、、、
以下のエラーが発生していました。

$ brew -v
zsh: command not found: brew

パスが通ってないことが原因となるため、Homebrewのパスを通すことにより、解決できます。

# 出力
Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Installation successful!

中略

==> Next steps:
- Run these commands in your terminal to add Homebrew to your PATH:
    echo >> /Users/{UserName}/.zprofile
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/{UserName}/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

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

$ echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/{UserName}/.zprofile
$ eval "$(/opt/homebrew/bin/brew shellenv)"

これで、Homebrewのバージョンを確認します!

$ brew -v
# 出力
Homebrew 4.4.16

2.HomebrewでGitをインストール

brewでGitをインストールするため、以下のコマンドを実行します。

$ brew install git

インストールが完了したら、Gitのバージョンを確認してみましょう!

$ git --version
# 出力
git version 2.39.5 (Apple Git-154)

3.Gitアカウントの設定(git config)

これを設定していないと、ローカルリポジトリからリモートリポジトリにpushする度に、
アカウントの情報を入力する必要があります。

最初に設定しておくと便利です!

gitのユーザー名、メールアドレスを以下のコマンドで設定します。

$ git config --global user.name "{GitUserName}"
$ git config --global user.email {GitUserEmail}

登録内容を確認したい場合は、以下のコマンドを実行してください。

$ git config --list

≈4.GitのSSH接続を設定
自分のパソコンからサーバーに接続しているよー!と安全に通信をおこなう一つの手段として、
SSH公開鍵による認証をする必要があります。

まず、SSHキーが存在するのかを確認します。

$ cat ~/.ssh/id_rsa.pub
# 出力
cat: /Users/xxxx/.ssh/id_rsa.pub: No such file or directory

ディレクトリが存在しない場合、SSH公開鍵を作成します。

$ ssh-keygen
or
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

SSHキーをSSHエージェントに追加します。
GitHubなどのリモートサーバーとの安全な接続を確立するために使用されるそうです!

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa

生成されたSSH公開鍵をコピーします。
以下のコマンドを実行すると、SSH公開鍵をクリップボードにコピーしてくれるので便利です。

$ cat ~/.ssh/id_rsa.pub | pbcopy

コピーした公開鍵をGithubに登録します。
登録方法は以下となります。
1.「自分のGithubプロフィール」>「Settings」>「SSH and GPG keys」で「New SSH key」に移動
2.コピーした公開鍵を登録

最後に

ここまですれば、Git初期設定は一通り完了です!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?