R.A. Epigonos et al.

[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 push origin 
Enter passphrase for key '/*******************':
ERROR: Repository not found.
fatal: The remote end hung up unexpectedly

既存の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

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

任意コミットに含まれるファイルを出力

$ git show ****************************************:path/to/file.txt > path/to/file.txt_

pull用リポジトリ(origin/master)とpush用リポジトリ(github/translation_ja)を設定

origin/masterをチェックアウトしてローカルにtranslation_jaブランチを作る。

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at f82fe29... core: update the message displayed on crash
$ git checkout -b translation_ja
Switched to a new branch 'translation_ja'
$ git branch --set-upstream translation_ja origin/master
Branch translation_ja set up to track remote branch master from origin.

push用リポジトリを登録して、そこの適当なブランチにpushする

$ git remote add github git@github.com:l/weechat.git
$ git push github translation_ja
Enter passphrase for key '********************':
Counting objects: 21, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 12.89 KiB, done.
Total 11 (delta 10), reused 0 (delta 0)
To git@github.com:l/weechat.git
 * [new branch]      translation_ja -> translation_ja
Branch translation_ja set up to track remote branch translation_ja from github.

登録したpull用リポジトリからpullする

$ git pull

作業用ブランチ(translation_ja_work)で作業、push用ブランチ(translation_ja)からpush

かなり頻繁にアップデートされるリポジトリからpullして、かなり頻繁に自分のcommitを追加。翻訳とかの場合だとそういう活動になる。でも本家のリポジトリにそんなちょぼちょぼしたcommitを入れると commit スパムになりかねない。というわけで、作業用ブランチとpush用ブランチを分けて、作業用で頻繁に翻訳結果をcommit。それなりに溜まったらpush用ブランチに作業用ブランチをmerge --squashして一つにまとめたcommitとしてcommitする。

作業用ブランチを作成

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2dcacc2... Merge pull request #37 from Mkaysi/patch-1
$ git checkout -b translation_ja_work
Switched to a new branch 'translation_ja_work'
$ git branch --set-upstream translation_ja_work origin/master
Branch translation_ja_work set up to track remote branch master from origin.

いろいろ作業

$ vi po/ja.po
$ git commit -a -vv -m 'translation: update'
$ vi doc/ja/weechat_user.ja.txt
$ git commit -a -vv -m 'translation: update'
...

push用ブランチを作成

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2dcacc2... Merge pull request #37 from Mkaysi/patch-1
$ git checkout -b translation_ja
Switched to a new branch 'translation_ja'
$ git branch --set-upstream translation_ja origin/master
Branch translation_ja set up to track remote branch master from origin.

作業用ブランチとpush用ブランチでorigin/masterをpullして最新状態にしておく。さらにgit diffでtranslation_jaからみたtranslation_ja_workとの違いを確認。

$ git checkout translation_ja_work
$ git pull
$ git checkout translation_ja
$ git pull
$ git diff --name-only translation_ja_work
doc/ja/weechat_dev.ja.txt
doc/ja/weechat_faq.ja.txt
doc/ja/weechat_plugin_api.ja.txt
po/ja.po

作業用ブランチの内容をmergeする。ただしその際に --squash をつけて、作業用ブランチの全てのcommitをpush用ブランチでは1つのcommitにまとめてmergeする。push前にログの確認。push用ブランチでpush。最後に作業用ブランチに戻る。

$ git merge --squash translation_ja_work
Updating 2dcacc2..187e8dc
Fast-forward
Squash commit -- not updating HEAD
 doc/ja/weechat_faq.ja.txt        |   16 +++++-----
 doc/ja/weechat_plugin_api.ja.txt |    5 ++-
 doc/ja/weechat_user.ja.txt       |  466 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------
 po/ja.po                         |   86 ++++++++++++++++++++++++++--------------------------
 4 files changed, 281 insertions(+), 292 deletions(-)
$ git commit -a -vv -m 'core: update Japanese translations'
$ git log --color --stat --pretty=fuller -1
commit 1e3010cd6f1c314df6d32bebb82f8a45ff1143d2
Author:     AYANOKOUZI, Ryuunosuke <i38w7i3@yahoo.co.jp>
AuthorDate: Sun Mar 23 06:11:49 2014 +0900
Commit:     AYANOKOUZI, Ryuunosuke <i38w7i3@yahoo.co.jp>
CommitDate: Sun Mar 23 06:11:49 2014 +0900

    core: update Japanese translations

 doc/ja/weechat_faq.ja.txt        |   16 ++--
 doc/ja/weechat_plugin_api.ja.txt |    5 +-
 doc/ja/weechat_user.ja.txt       |  466 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------------------------
 po/ja.po                         |   86 +++++++++++-----------
 4 files changed, 281 insertions(+), 292 deletions(-)
$ git push github translation_ja
Enter passphrase for key '********************':
Counting objects: 890, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (312/312), done.
Writing objects: 100% (649/649), 1.25 MiB, done.
Total 649 (delta 554), reused 411 (delta 337)
To git@github.com:l/weechat.git
   568c038..1e3010c  translation_ja -> translation_ja
$ git checkout translation_ja_work
Switched to branch 'translation_ja_work'
Your branch is ahead of 'origin/master' by 10 commits.

詳細なログを見る

$ git log --color --stat --pretty=fuller

無視したいファイルのリスト

.gitignoreに書く。githubにあるテンプレートは使える。

translation_ja_workで管理しているファイルをtranslation_ja_testにmergeする際にこれを除外する

$ git checkout translation_ja_work
$ vi doc/ja/weechat_relay_protocol.ja.txt
$ git add doc/ja/weechat_relay_protocol.ja.txt
$ git diff --name-only origin/master
doc/ja/weechat_relay_protocol.ja.txt
po/ja.po
$ git commit -a -vv -m "translation: update"
$ git checkout translation_ja_test
$ git pull
$ git merge --nocommit --squash translation_ja_work

git管理対象外ファイルおよびディレクトリを削除

ビルドすると中間ファイルができる。この中間ファイルがいろいろ邪魔をしたりすることがあるので削除。

git clean -n で削除対象を確認する。-dオプションを付ければディレクトリも削除対象に含める。実際の削除は-nの代わりに-fオプションを付けで行う。

$ git clean -n
Would not remove build/
Would remove lang/ja_jis.l
Would remove lang/ja_utf8.l.old
Would remove lang/ja_utf8.l.old0
Would remove lang/langcheck.log
Would not remove language/
$ git clean -f
Not removing build/
Removing lang/ja_jis.l
Removing lang/ja_utf8.l.old
Removing lang/ja_utf8.l.old0
Removing lang/langcheck.log
Not removing language/
$ git clean -n -d
Would remove build/
Would remove language/
$ git clean -f -d
Removing build/
Removing language/

強制削除オプション-fを渡さないと以下のように言われる。

$ git clean -d -x
fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean

gitignoreで無視されているファイルも削除するには-xオプションを渡す。

$ git clean -n -d -x
Would remove Makefile
Would remove config.cache
Would remove config.log
Would remove config.status
Would remove include/language.h
Would remove include/sysconf.h
Would remove include/version.h
Would remove lang/cat
Would remove lang/de
Would remove lang/en_us
Would remove lang/es
Would remove lang/fr
Would remove lang/gr
Would remove lang/hun
Would remove lang/index
Would remove lang/it
Would remove lang/ja_utf8
Would remove lang/langcomp
Would remove lang/language.h
Would remove lang/nl
Would remove lang/pl
Would remove lang/pt
Would remove lang/ru
Would remove lang/tr
(snip)
Would remove src/bin/anoperc
(snip)
Would remove src/core/Makefile.inc
(snip)
Would remove src/core/bs_act.s
Would remove src/core/bs_act.so
Would remove src/core/bs_assign.o
(snip)
Would remove src/tools/anopesmtp
Would remove src/tools/db-merger
Would remove src/tools/epona2anope

リファレンス

  1. git remote - Google 検索
  2. git-remote(1)
  3. gittutorial(7)
  4. git - Is git show safe to use on binary files when the output is redirected to a file? - Stack Overflow
  5. Git: merge all changes from another branch as a single commit - Stack Overflow
  6. git branch merge single - Google 検索
  7. github/gitignore · GitHub

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2009-02-19T06:39:59+09:00
  2. Modified: 2009-02-19T06:39:59+09:00
  3. Modified: 2012-09-26T02:52:20+09:00
  4. Generated: 2023-08-27T23:09:15+09:00