綾小路龍之介の素人思考

[github] gitの使いかた

gitはなかなか使えるものだということがわかったので。

新規に作成したgitリポジトリをgithubで共有する

まずはgithubのページから適当なプロジェクト(gitの言葉でいえばリポジトリ)を作る。ここでは仮にexampleというgithubアカウントでtestというプロジェクトを作ったとする。webからプロジェクトを作ったらこれ以上web上でやることは無い。そして、プロジェクトという概念も忘れて良い。なぜなら後はgitの使い方そのものだから。

$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /home/hoge/test/.git/
$ ls -la
total 12
drwxr-xr-x 3 hoge hoge 4096 2009-12-20 03:32 .
drwxr-xr-x 6 hoge hoge 4096 2009-12-20 03:31 ..
drwxr-xr-x 7 hoge hoge 4096 2009-12-20 03:32 .git
$ touch README
$ git add README
$ git commit -m 'first commit'
$ git remote add origin git@github.com:example/test.git
$ git push origin master

既存のgitリポジトリをgithubで共有する

新規に作成したリポジトリを共有できるのなら、既存のリポジトリを共有するのはもっと簡単。/path/to/existing_git_repo内のリポジトリをexampleというgithubアカウントのtesuというプロジェクトで共有してみる。新規作成との違いは、既存のリポジトリでは既にgit initが終了して何回かcommitを終えているということ。つまり、gitリポジトリに移動して、git remote addからはじめる。

$ cd /path/to/existing_git_repo
$ git remote add origin git@github.com:example/test.git
$ git push origin master

fatal: remote origin already exists.

originという名前はどこのマニュアルにも載っているので、リモートリポジトリを追加する場合に、下のようなエラーが出る場合があるかもしれない。

$ git remote add origin git@github.com:example/test.git
fatal: remote origin already exists.

このエラーメッセージは既に同じ名前でリモートリポジトリが登録されている事が原因で出る。その場合、まずは登録されているリモートリポジトリの名前とURLを確認する。

$ git remote -v
origin  git@github.com:example/test.git

originという名前でリモートリポジトリが登録されているので、重複しないように名前を変えれば登録できる。

$ git remote add origin2 git@github.com:example/test.git
$ git remote -v
origin  git@github.com:example/test.git
origin2 git@github.com:example/test.git

リモートリポジトリの削除

同じURLでいくつも別名を持たせることは意味のあることだけど、あんまりたくさんあってもしょうがないでしょ。ということで、削除する方法。

$ git remote -v
origin  git@github.com:example/test.git
origin2 git@github.com:example/test.git
$ git remote rm origin2
$ git remote -v
origin  git@github.com:example/test.git

ファイルやディレクトリの追加

管理したいファイルとディレクトリを追加するにはgit addコマンドを使う

$ mkdir perl
$ touch perl/hote.pl
$ git add perl
$ git commit
$ git push github

リモートリポジトリの追加

リモートリポジトリが複数ある場合もある。そのような場合は追加しておく。例えばgithubで自分用にforkしたリポジトリと、大本のリポジトリ。自分用リポジトリのコミット権限はあるけども、大本のリポジトリのコミット権限は無い。だから、自分で開発する場合は自分用のリポジトリにコミットしていくんだけど、これを大本にマージして欲しいときには大本の変更を全て取り込んだ上でマージ要求を出した方がいい。

まずは大本リポジトリの追加

$ git remote add hoge git://github.com/hoge/fuga.git

そして、大本リポジトリの内容をpullする。

$ git pull hoge master
From git://github.com/hoge/fuga
 * branch            master     -> FETCH_HEAD
Auto-merged **********
CONFLICT (content): Merge conflict in **********
Automatic merge failed; fix conflicts and then commit the result.

マージ失敗したので手作業で衝突の回避。

$ vi **********
$ git pull zigdon master
You are in the middle of a conflicted merge.

変更内容をコミットしてpullする。

$ git commit
Created commit 4abc3a3: Merge branch 'master' of git://github.com/hoge/fuga
$ git pull zigdon master
From git://github.com/hoge/fuga
 * branch            master     -> FETCH_HEAD
Already up-to-date.

pullして、pushする

2つのリポジトリで1つが本家、1つが自分用コピーの場合。自分用コピーで仕事を始める前に必ずpullして、自分用コピーに本家の変更を取り込んでおく。その後に自分用コピーを編集して仕事を行う。これが一つの手順。

まずは本家からpull。

$ git pull ****** master
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 18 (delta 12), reused 0 (delta 0)
Unpacking objects: 100% (18/18), done.
From git://github.com/******/*******
 * branch            master     -> FETCH_HEAD
Auto-merged **********
Merge made by recursive.
 ********** |  221 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 176 insertions(+), 45 deletions(-)

その後に自分用コピーにpush。

$ git push origin master
Enter passphrase for key '/home/**************************************':
Counting objects: 25, done.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (21/21), 4.35 KiB, done.
Total 21 (delta 14), reused 0 (delta 0)
To git@github.com:*/*******.git
   57a7cd8..7de4852  master -> master

この後に自分用コピーの上で作業する。

リファレンス

  1. git remote - Google 検索
  2. git-remote(1)
  3. gittutorial(7)

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us
  4. livedoor クリップ
  5. Yahoo!ブックマーク
  6. @niftyクリップ
  7. newsing

ChangeLog

  1. Posted: 2009-02-19T06:39:59+09:00
  2. Modified: 2009-02-19T06:39:59+09:00
  3. Generated: 2011-12-31T03:45:12+09:00