git初心者がmacでgithubを使うまでのTips – githubのお勉強

git初心者がmacでgithubを使うまでのTips – githubのお勉強

バージョン管理もそろそろしてみようということでgitを選択。分散型でいいらしい。git自体まだ分かっていないけど、github使ってみようという試み。

環境:mac OSX 10.6

公開鍵方式でSSHログイン出来るようにする

どうやらgithubにpushするにはSSH公開鍵を登録する必要がある様子。なので、秘密鍵と公開鍵のペアの作成から。

秘密鍵と公開鍵のペアを作成

ターミナルで秘密鍵と公開鍵のペアを作成する。RSA方式でよいと思われる。
作業する場所はユーザーのホームディレクトリ。

ターミナルで ssh-keygen -t rsa で作成できる。
パスフレーズは空でenterする。

$ ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/clicktx/.ssh/id_rsa): [enter]
Enter passphrase (empty for no passphrase): [enter]
Enter same passphrase again: [enter]
Your identification has been saved in /Users/clicktx/.ssh/id_rsa.
Your public key has been saved in /Users/clicktx/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx clicktx@MacBook.local
The key's randomart image is:
+--[ RSA 2048]----+
| o+.  o.+        |
|  o= o B .       |
|  o + = o .      |
|   o + o o .     |
|    o = S . .    |
|     o o     .   |
|      .       E  |
|                 |
|                 |
+-----------------+

id_rsa ... 秘密鍵
id_rsa.pub ... 公開鍵

ユーザーホームディレクトリに .ssh というディレクトリが作られて、その中に秘密鍵と公開鍵のペアが作成される。

公開鍵を github に登録する

公開鍵である id_rsa.pub の内容をgithubに登録する。id_rsa.pubの内容はターミナルなどから確認できる。

$ cd .ssh
$ cat id_rsa.pub 
ssh-rsa Axxxxx.....
......
...../BxxxxxxJxxxxx== clicktx@MacBook.local

id_rsa.pub の内容の

ssh-rsa Axxxxx.....
......
...../BxxxxxxJxxxxx== clicktx@MacBook.local

の部分をコピーしてgithubに登録する。

github-Add another public key
github-Add another public key

SSHの接続テスト

githubに公開鍵を登録したら、接続できるかテストしてみる。接続するか聞かれるので yes とタイプしてenter。
Hi username!.... となれば接続成功。

$ ssh -T git@github.com
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 00:00:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
Hi clicktx! You've successfully authenticated, but GitHub does not provide shell access.

githubでリポジトリ作成

試しに git-test というリポジトリを作成してみる。githubのログインページに「New Repository」があるのでそこから作成する。
Project Nameにgit-testと入力してCreate Repositoryする。

github-Create a New Repository
github-Create a New Repository

作成すると次のステップとか、リポジトリのpathとかが表示される。
github-New Repository

ローカルリポジトリの作成

とりあえず、先ほど作成したgithubの手順のまま実行してみる。
(gitのインストールと設定はしてある前提で...;)

管理するファイルを作る

適当な場所(~/Docmentsとか)で作業する。
管理したいファイルを入れるディレクトリ(git-test)を作って、gitの初期化を行う。

$ mkdir git-test
$ cd git-test/
$ git init
Initialized empty Git repository in /Users/clicktx/Documents/git-test/.git/

テストファイル(とりあえずREADME)を作る。

$ touch README

gitの操作

作ったファイル(README)をステージ(add)してからcommitする。

$ git add README
$ git commit -m 'first commit'
[master (root-commit) 2093860] first commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

リモートのショートネームをデフォルトのショートネーム origin で登録する。

$ git remote add origin git@github.com:clicktx/git-test.git

リモートであるgithubのリポジトリにpushする。

$ git push -u origin master

成功するとこんな感じでアクセスできるようになる。
http://github.com/clicktx/git-test

参考:

  • はじめてのgithub - blog.katsuma.tv
  • Help.GitHub - Set Up Git