git tips

■共有リポジトリを作成

$ mkdir repo.git
$ cd repo.git
$ git init --bare --shared=true

git init --bare --shared
git init --bare --shared=group

も同じ意味、グループ属性にsビットを設定し、同じグループのユーザが共有できるようになる。

従って、repo.git 以下を全て共通の user グループにする。

$ cd ..
$ chgrp -R user repo.git

■ git 専用のユーザは git-shell を使う

git:x:1001:1001:Git,,,:/home/git:/usr/bin/git-shell

■ローカルからリモートリポジトリを最初に使うには

$ git init
$ git remote add repo ssh://server/home/git/repo.git (公開リポジトリの名前を repo とする)
$ git add . (このディレクトリを追加)
$ git commit -m 'initial import' (ローカルにコミット)
$ git push repo master (master ブランチを repo に push)

■リモートリポジトリ名(repo)の URL を確認する

$ git remote show repo
$ git remote -v (複数のリモートリポジトリを表示する)

■リモートリポジトリを clone する

$ git clone ssh://server/home/git/repo.git (空のリポジトリは clone できない)

■ ~/.git_config の設定

$ git config --global user.name 'your name'
$ git config --global user.email 'mail@address'

(~/.git_config に名前とメアドが設定される。)

$ git config -l (config の内容が確認できる)

■svn -> git への引っ越し

git-svn を使用

Fedora の場合 yum install git-svn でインストール

以下のコマンドでローカルリポジトリを作成

$ git svn clone -s https://server/svn/repo/repo-svn

■svn のリモートブランチを tag 化しリモートリポジトリに push する

$ git branch -r (リモートブランチの一覧を取得)
$ git checkout tags/release-0.3.4_trunk (リモートブランチに移動)
$ git tag v0.3.4 (タグを付ける)
$ git tag

(タグが増えている)
v0.3.4
v0.3.5

$ git push origin v0.3.4 (tag をリモートブランチに push )
$ git push origin --tags (全てのタグを push する)

■別のローカルから tag を pull する

$ git pull origin --tags
$ git tag
v0.3.4
v0.3.5

■tag を消す

$ git tag -d v0.3.5

リモートの tag を消すには

$ git push origin :refs/tags/v0.3.5

■tag から branch を作る

$ git chekout -b <ブランチ名> <タグ名>

(↑は↓と同じ)

$ git branch <ブランチ名> <タグ名>
$ git checkout <ブランチ名>

■パッチ

パッチの作成

$ git-format-patch -r HEAD~ (パッチファイルが作成される)

format-patchで作られたパッチをあてる

$ git-am 0001-Changed-hoge.patch

diffで作られたパッチをあてる

$ git apply 001-Changed-hoge.patch

パッチをチェックする

$ git apply --check 001-Changed-hoge.patch

■CHANGELOG用に log をフォーマットする

git log --pretty=format:"* %s [%an] %h" > CHANGELOG

■タグの情報を見る

git show <タグ名>

■svnのように1ファイルだけリバート

$ git checkout <ファイル名>

もしも、<ファイル名> とブランチの名前が重なる場合は、

$ git checkout -- <ファイル名>

全部リバートしたい場合は、

$ git reset --hard

■過去のコミットからブランチを作る

$ git branch <ブランチ名> <コミット番号>
$ git chekout <ブランチ名>

■リポジトリからブランチを取得する

$ git pull --rebase
$ git branch -a (リモートブランチの取得を確認)
* master
origin/HEAD
origin/issue
origin/master

$ git checkout -b issue origin/issue (ローカルにissueブランチを作成)

$ git branch (ローカルにissueブランチができたことを確認)
* issue
master

■リモートブランチを削除する

$ git branch -d issue (ローカルブランチを削除)
$ git branch -r -d origin/issue (ローカルの追跡用ブランチを削除)
$ git push origin :issue (削除した空のブランチを push)
              (ブランチ名の前にコロンを付ける)

■ archive

v0.3.5 タグの内容を v0.3.5/ 以下にアーカイブする。

$ git archive --format=tar --prefix=v0.3.5/ v0.3.5 | gzip > v0.3.5.tar.gz

■ git add でステージしてしまったファイルを取り消す

$ git reset HEAD <ファイル名>

git status で取り消された事を確認する。


Latest update at 2010/3/28