nofuture.tv  Index  Search  Changes  RSS  Login

Gitメモ

使ってみる

Git入門のチュートリアルをそのまましてみます。

インストールしたもの

とりあえずgit-coreとGUIで変更点が見れたりするgitkとqgitをインストールしました。

# aptitude install git-core gitk qgit

最初の最初

プロジェクトがあるなら

$ git clone <url>

プロジェクトがないならディレクトリを作ります

$ mkdir project

プロジェクトディレクトリに降りて

$ cd project

初期化します。

$ git init

自分の名前とメールアドレスを登録する

$ git config --global user.name 名前
$ git config --global user.email メールアドレス

いろんなプロジェクトに参加している人は、iwamatsuさんのところに書いてあるみたいに環境変数に登録しておくとよいみたい。

手持ちのファイルを登録する

まるっと登録するなら

$ git add .

必要なものだけなら

$ git add file.txt example/

みたいにする。登録されたくないファイルなどは、プロジェクトのトップディレクトリに.gitignoreというファイルを作って外してほしいファイルを書いておく。正規表現も使えます。

登録したファイルを記録する

$ git commit

コミットメッセージを書くように催促されるので、ファイルなどの変更点を書きます。

良いコミットメッセージの書き方は、要点を短く記したタイトルを書いたあと、一行空行を開けて、詳しく変更点を書くといいそうです。

登録と記録を一度にするには

$ git commit -a

コミットと同時にコミットメッセージを登録するなら

$ git commit -m "コミットメッセージ"

変更点を見る

$ git log

ブランチを作ってマージする

大胆に変更したいときや開発版から安定版を作るときなど分岐させるときに使うらしい。

とりあえずブランチのリストを見てみる。

$ git branch
* master

ブランチがないので今はmasterだけ。 新しいブランチtestingを作ってみる。

$ git branch testing

ブランチを見てみる。

$ git branch
* master
  testing

ブランチをtestingに変更してから、ファイルをいじってコミットする。

$ git checkout testing
$ git commit -a

masterに戻って変更点を確認してみる。

$ git checkout master
$ git log

testingの変更したところが変わってない。

testingで変更したところをマージするには、そのまんまマージを使う。

$ git merge testing

コンフリクトしたらメッセージとか出るみたいだけど、今はないのでふつーにマージできた。 チュートリアルによるとGUIでも確認できるそうなので見てみる。

$ gitk

おお。分岐して繋がった。 ブランチの削除は-dオプションで、強制削除は-Dオプションだそうな。

$ git branch -d testing
$ git branch -D testing

違いはよくわからんけど。

pushとpullは

なんとなくはわかるけど一人プロジェクトに必要ないので一通りして飛ばしました。

違いを見る

コミットされたログを見るには

$ git log
commit 720a67f66a5638269b958c89e93a639296ee0e4a
Author: Jun NOGATA <jun@amy.nofuture.tv>
Date:   Mon Feb 4 15:19:24 2008 +0900

    changes filename

こんな感じ。コミットされた内容を詳しく見るには

$ git show 720a67f66a5638269b958c89e93a639296ee0e4a

こんなに打てるかぁ!てことで頭の何文字かを打てば出てくる。

$ git show 720a

だけどいっぱいコミットしたらこんだけじゃ済まないだろうな。 てことで簡略版。

$ git show HEAD (ブランチの最先端)

にしても親の意味がわからん。一つ前のことじゃなくて?

それはさておき、長くてうぜぇコミットに名前をつけることができます。それがタグ。

$ git tag v0.1 720a67

うーん。mercurialみたいにリビジョンがほしいところ。

git-resetとgit-revert

共同作業している場合、resetするとコミット自体がなくなってしまうのでresetしちゃいけないそうな。 巻き戻すにはgit-revertを使ってくれだそうです。

git log とかgit grepとか

で、いろいろ調べられると。


クイックスタート

from git wiki

Gitプロジェクトの設定

プロジェクトに参加するならクローンすることから始めます。

$ git clone <url>

参加しているGitプロジェクトがなければ作成します:

$ cd project/
$ git init          # initializes the repository
$ git add .         # add those 'unknown' files
$ git commit        # commit all changes, edit changelog entry

Git will look for a file named .gitignore in the root of your repository which contains a set of shell patterns to ignore in file paths.

Gitはリポジトリのルート、もしくは にある.gitignoreというファイル名を探すでしょう。

ブランチとマージ

$ git checkout -b linux-work        # "linux-work"と名づけた新しいブランチを作る
$ <編集する>
$ git commit -a
$ git checkout master               # masterブランチに戻します。
$ git merge linux-work              # merge changesets from linux-work (Git >= 1.5)
$ git pull . linux-work             # merge changesets from linux-work (all Git versions)

パッチをインポート

$ git apply < ../p/foo.patch
$ git commit -a

パッチをエクスポート

$ <make changes>
$ git commit -a -m "commit message"
$ git format-patch HEAD^  # creates 0001-commit-message.txt
                          # (HEAD^ means every patch since one revision before the
                          # tip of the branch, also known as HEAD)

ネットワークサポート

# プライマリのGitリポジトリからクローン
foo$ git clone git://git.kernel.org/pub/scm/git/git.git
foo$ cd git

# pushing changes to a remote repo with SSH
foo$ git push user@example.com:my-repository.git/

# fetch changes to a remote branch into a local branch
foo$ git fetch user@example.com:my-repository.git/ remote-branch:local-branch

# merge changes from a remote machine
bar$ git pull git://foo/repo.git/ branch

# Serve repository via git protocol
foo$ cd /my/repository/
foo$ touch .git/git-daemon-export-ok
foo$ git daemon  # now others can fetch from git://your.machine/my/repository/.git/

# Set up a bare (= without working directory) repository (e.g. on a webserver)
foo$ mkdir my-repo.git
foo$ cd my-repo.git
foo$ git --bare init
foo$ chmod a+x hooks/post-update # this is needed for HTTP transport
                                      # you need to populate this repository via push

リビジョンを見る

# inspect history visually
foo$ gitk       # this opens a Tk window, and shows you how the revisions are connected

# inspect history
foo$ git log    # this pipes a log of the current branch into your PAGER
foo$ git log -p # ditto, but append a patch after each commit message

# inspect a specific commit
foo$ git show HEAD    # show commit info, diffstat and patch
                      # of the tip of the current branch

リビジョンを調べる

# by name
foo$ git log v1.0.0   # show history leading up to tag "v1.0.0"
foo$ git log master   # show history of branch "master"

# relative to a name
foo$ git show master^   # show parent to last revision of master
foo$ git show master~2  # show grand parent to tip of master
foo$ git show master~3  # show great grand parent to tip of master (you get the idea)

# by output of "git describe"
foo$ git show v1.4.4-g730996f  # you get this string by calling "git describe"

# by hash (internally, all objects are identified by a hash)
foo$ git show f665776185ad074b236c00751d666da7d1977dbe
foo$ git show f665776   # a unique prefix is sufficient

# tag a revision
foo$ git tag v1.0.0                      # make current HEAD known as "v1.0.0"
foo$ git tag interesting v1.4.4-g730996f # tag a specific revision (not HEAD)

リビジョンを比較する

# diff between two branches
foo$ git diff origin..master            # pipes a diff into PAGER
foo$ git diff origin..master > my.patch # pipes a diff into my.patch

# get diffstat of uncommitted work
foo$ git diff --stat HEAD

Cherry picking patches

foo$ git cherry-pick other-branch~3     # apply 4th last patch of other-branch to current branch
Last modified:2008/02/04 22:09:08
Keyword(s):
References:
This page is frozen.