GitHub上でリポジトリを作成する。
GitHubのGUI上でリポジトリを作成してください。New Repositoryを選択し、リポジトリ名などを入力するだけです。
■ユーザ名・メールアドレス設定
→公開していいユーザ名・メールアドレスを設定する
# git config --global user.name "hogehoge"
# git config --global user.mail "hogehoge@example.com"
■設定内容確認
# more .gitconfig
[user]
name = hogehoge
mail = hogehoge@example.com
■コマンド出力を読みやすくする設定
# git config --global color.ui auto
# more .gitconfig
[user]
name = hogehoge
mail = hogehoge@example.com
[color]
ui = auto
■RSAキー作成
# ssh-keygen -t rsa -C "hogehoge@example.com"
→最初の部分は空Enter。
パスフレーズは覚えられるものを指定しよう。
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx hogehoge@example.com
The key's randomart image is:
+--[ RSA 2048]----+
| ....o.... |
| xxxxxx... |
| xxxxx.... |
| . *.xxx . x |
| .S .E=|
| .o.|
| + |
| o |
| |
+-----------------+
■~/.ssh/id_rsa.pubが公開鍵なので、これをGithub上のSSH Keyとして登録します。
# cat id_rsa.pub
ssh-rsa
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
== hogehoge@example.com
これが終わったら、GitHub上でのGUI操作でSSH Keyを登録してください。
■接続確認
# ssh -T git@github.com
The authenticity of host 'github.com (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is xxxxxxxxxxxxxxxxxxxxxxxxxx4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,xxx.xxx.xxx.xxx' (RSA) to the list of known hosts.
Enter passphrase for key '/root/.ssh/id_rsa':
Hi hogehoge! You've successfully authenticated, but GitHub does not provide shell access.
# echo $?
1
→echoの結果は「1」ですが、上記のようなメッセージが出ていれば、接続はOKです。
■作成したリポジトリをcloneします。
# mkdir git
# cd git/
# git clone https://github.com/hogehoge/hogehoge.git
Initialized empty Git repository in /root/git/hogehoge/.git/
warning: You appear to have cloned an empty repository.
# ls
hogehoge
→Github上のリポジトリができています。
# cd hogehoge/
# ls
LICENSE README.md
★GitHubにコードを公開する
■コードの作成
→コードを作っておいてください。以下はサンプルです。
# cat test_world.php
<?php
echo "Test World!";
?>
■確認
→Gitリポジトリに登録されていない状況(Untracked files)
# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test_world.php
nothing added to commit but untracked files present (use "git add" to track)
■ステージ(コミットする前のファイル状態を記録したインデックスにデータ構造を記録)
# git add test_world.php
# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: test_world.php
#
●ディレクトリ毎ステージするには…
"."で指定すれば、サブディレクトリ毎ステージすることができる。
# git add .
# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bin/zabbix-check.sh
# new file: etc/master
# new file: etc/trans.txt
# new file: log/zabbix-check.log
# new file: sql/items.error.sql
#
■コミット
# git commit -m "Add Test World script by php"
[master xxxxxxx] Add Test World script by php
Committer: hogehoge <hogehoge@hogehoge.com>
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:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <you@example.com>'
1 files changed, 3 insertions(+), 0 deletions(-)
create mode xxxxxxx test_world.php
■ログ確認
# git log
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: hogehoge <hogehoge@hogehoge.com>
Date: Sat Jan 2 08:22:41 2016 +0900
Add Test World script by php
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author: hogehoge <hogehoge@hogehoge.com>
Date: Sat Jan 2 07:58:51 2016 +0900
Initial commit
■pushする(GitHub側のリポジトリ更新)
→エラーが出た場合は、エラー例を参照
# git push origin master
Password:
Counting objects: 4, done.
Delta compression using up to 3 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 326 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://hogehoge@github.com/hogehoge/hogehoge.git
xxxxxxx..xxxxx master -> master
GitHubのGUIが更新されていることを確認する。
よくあるエラー例
■push時のエラー
# git push
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/hogehoge/hogehoge.git/info/refs
fatal: HTTP request failed
これは、configの設定で、URLにユーザ名@github.comとなっていない場合、こうなる。以下を実行する
●git remoteの実施
# git remote set-url origin https://hogehoge@github.com/hogehoge/hogehoge.git
●configの確認
# more config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://hogehoge@github.com/hogehoge/hogehoge.git ★確認
[branch "master"]
remote = origin
merge = refs/heads/master
●再実行
# git push origin master
Password:
Counting objects: 4, done.
Delta compression using up to 3 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 326 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://hogehoge@github.com/hogehoge/hogehoge.git
xxxxxxxxx..xxxxxx master -> master
成功!