github利用ガイド git備忘記録
概要
さきほどgithubデビューをしたので、gitの使い方(githubレポジトリへの反映方法)などの備忘記録を兼ねて、githubの利用ガイドを簡単に書いてみました。需要はないかもしれませんが、誰かの参考になれば幸いです。
githubでの作業
アカウントが必要です。作りましょう。github
公開レポジトリ作成(github)
ログイン状態でいれば"Create a Repository"のリンクがあるのでクリックしてレポジトリを作りましょう。
こんな感じで作成しました。以下のURL等を用いてレポジトリを操作します。
- git@github.com:Kshi-Kshi/test.git
- https://Kshi-Kshi@github.com/Kshi-Kshi/test.git
- git://github.com/Kshi-Kshi/test.git (Read Only)
公開鍵を作成する
- 使用するサーバ側で公開鍵を作成(~/.ssh/以下に配置)
- 設置場所はデフォルトでOKなので最初の問はそのままクリック。
- 次の問いのパスワードは各自お好きに。
$ ssh-keygen -t rsa -C "saibaiman2011@gmail.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/kshi_kshi/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/kshi_kshi/.ssh/id_rsa. Your public key has been saved in /home/kshi_kshi/.ssh/id_rsa.pub. The key fingerprint is: 80:a6:88:50:aa:af:75:24:3b:19:d7:31:b7:c4:dd:ff saibaiman2011@gmail.com
が作成される。この公開鍵ファイル(id_rsa.pub)の内容をgithubに登録します。
- githubでの作業
github: "アカウントの設定"->"SSH公開鍵"的なリンクから登録します。
catかなんかで出力させて公開鍵ファイルをコピーしましょう。
以下登録例。
"キーの追加"ボタンを押して怒られなければOK。
- 確認
設定した内容が正しいかどうか、確認できるので、確認しましょう。
公開鍵を作成して設置したサーバ側での処理
$ ssh git@github.com The authenticity of host 'github.com (207.97.227.239)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. 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. Enter passphrase for key '/home/kshi_kshi/.ssh/id_rsa': Hi Kshi-Kshi! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.
OK.
これにて、githubでの作業は終了。
git関連
- gitとは
Git(ギット)はプログラムなどのソースコード管理を行う分散型バージョン管理システム。動作速度に重点が置かれている。Linuxカーネルのソースコード管理を目的として、リーナス・トーバルズによって開発された。現在のメンテナンスは濱野純 (Junio C Hamano)が担当している。
http://ja.wikipedia.org/wiki/Git
Subversionは集中型バージョン管理システムであるのに対し、gitはローカルでレポジトリを持てる、派生物の作成が容易などメリットが盛りだくさんとのこと。
しかし、複数のレポジトリが存在しているというのは複数人で開発するとき、同期が大変なのではないかと思うが、マスターレポジトリー以外に対してpush/pullを行わないなどの一定のルールを定めることで集中型バージョンシステムのように使えるということ。このように魅力的なバージョン管理システムみたい。
集中型の方がイメージしやすいので利用シーンに応じて使い分けることのがベストかと。でもローカルでレポジトリが持てるのは一番のメリットのような気がする。
gitインストール(1.7.4.1)
開発するサーバにgitを入れておく必要があるので導入。
- 環境
ディストリビューション | Red Hat Enterprise Linux Server release 5.6 (Tikanga) |
- インストール作業 (最新版はコチラ)
$ wget http://kernel.org/pub/software/scm/git/git-1.7.4.1.tar.bz2 $ tar -x -f git-1.7.4.1.tar.bz2 -j -v $ cd git-1.7.4.1 $ ./configure $ make $ sudo make install
git設定〜レポジトリ作成
- git環境の設定
$ git config --global user.name "Kshi_Kshi" $ git config --global user.email saibaiman2011@gmail.com
- レポジトリ作成(git init)
管理したいディレクトリまで行き、"git init" する
$ cd /home/kshi_kshi/public_html/git_test $ git init Initialized empty Git repository in /home/kshi_kshi/public_html/git_test/.git/
- 設定の確認
$ git config --list user.name=Kshi_Kshi user.email=saibaiman2011@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true
個別で確認することも可能
$ git config user.name Kshi_Kshi
/etc/gitconfigでシステム上のすべてのユーザーとすべてのリポジトリに対する設定もできるらしい。 Pro Git
git add〜commit
レポジトリ内での作業。
- 状態確認(git status)
"svn st" みたいな。
$ touch README $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track)
- 管理したいファイルをレポジトリに追加する(git add)
"svn add" みたいな。
$ git add README $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README #
- ローカルレポジトリに反映させる(git commit)
"svn commit" はメインのレポジトリに反映させるが、"git commit" はローカルのレポジトリに反映させる。(他のレポジトリでは影響なし)
以下は"git commit"をした時の例。-mオプションを付けない場合は 設定ファイルの core.editor にて指定したエディターが起動し、コメントをつけられる。(指定しない場合は環境変数$EDITORが起動する.)
$ git commit [master (root-commit) 9941a91] create README 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 README
- ファイルの編集
commitした後で(厳密にはadd以降)ファイルに変更を加えてレポジトリに反映させたい時は、反映させたいファイルを"git add" してからcommitする必要がある。svnでは一度addした後は、commitすればすべて反映されるが、gitは反映したいファイルを細かく指定することが可能という柔軟性をもたせるためこのようになっているんだと思われる。
$ echo '追加します' >> README $ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README # no changes added to commit (use "git add" and/or "git commit -a") $ git add README $ git commit -m 'modified README'
また、"git add" した以降に同じファイルを編集してしまうとaddした時点までの状態までしか反映されないので注意が必要。
しかし、毎回addするのが面倒だということであれば、"git commit -a" みたいに-aオプションをつけることで、管理しているファイルに変更があったファイルすべて反映させることができる。
$ git commit -a -m 'README追記しました' [master f0c6787] README追記しました 1 files changed, 2 insertions(+), 0 deletions(-)
詳しくココ→Pro Git
git remote〜git push
- ローカル以外のレポジトリの設定"git remote"
デフォルトはoriginという名をつけるのが慣習らしい。
$ git remote add origin git@github.com:Kshi-Kshi/test.git $ git remote origin
もちろん分散型バージョン管理システムなので、リモート先のレポジトリの追加もできる。
$ git remote add second git@github.com:Kshi-Kshi/test2.git $ git remote origin second
-vオプションで詳しく表示
$ git remote -v origin git@github.com:Kshi-Kshi/test.git (fetch) origin git@github.com:Kshi-Kshi/test.git (push) second git@github.com:Kshi-Kshi/test2.git (fetch) second git@github.com:Kshi-Kshi/test2.git (push)
- リモート先のレポジトリに反映させる(git push)
$ git push origin master
ここでのoriginはremoteで指定したレポジトリを指している。masterとは反映させるブランチのこと。
- リモーと先の変更点をローカルレポジトリに反映させる(git pull)
$ git pull origin
その他にも様々なコマンドがあるみたいですが、私自体が把握できていないので、ここまでとしました。
詳しくは Pro Git にとても詳しく書いてあるので参考にして頂ければと思います。
ざっくりとしたgitの流れ(レポジトリを1から作る場合)
- gitの初期設定 (git config)
- レポジトリ作成 (git init)
- 外部レポジトリを定義 (git remote add)
- ファイル作成->管理ファイルとする (git add)
- ローカルへ反映 (git commit)
- ローカルレポジトリの内容を外部レポジトリに反映(git push)
- ファイル編集->ローカルへ反映(git add, git commit | git commit -a)
- 外部レポジトリの変更点をローカルファイルに反映(git pull)
- 外部へ反映(git push)
こんな感じで開発して行くのでないかなと想定しています。