subversionからの移行

さすがに使う機会が増えてきたのでなれるべく一部移行。下記ドキュメントが良い。

http://d.hatena.ne.jp/idesaku/20090323/1237825080

用語集

難解かつ耳慣れない言葉ばかり

bareリポジトリ管理用のファイルのみ、ワーキングディレクトリを持たないリポジトリ。
non-bareリポジトリワーキングディレクトリを持つ
mirrorリポジトリbareリポジトリのリモートリポジトリバージョン

インストール

CentOS6系だとあまりに古いバージョンが入るのでコンパイルしたほうが良いだろう。

コンパイル

#必要ライブラリインストール
yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
#ソースダウンロード
wget https://www.kernel.org/pub/software/scm/git/git-2.4.10.tar.gz
tar zxvf git-2.4.10.tar.gz
cd git-2.4.10
# コンパイル
make prefix=/usr/local all
make prefix=/usr/local install

参考サイト

https://try.github.io/levels/1/challenges/1

新しいブランチを作ってコミットするまで

git checkout -b feature/mynewbranch
git push origin feature/mynewbranch -n
git push origin feature/mynewbranch

チェックアウト手順

クローンする

git clone http://example.com/git 任意のディレクトリ名
cd 任意のディレクトリ名
git config --global http.sslVerify false

pullする

git pull origin testing

コマンド版 gitローカルリポジトリ作成

git init
git clone http://example.com/hoge/git.git [hoge以外の名前を付けたい場合]
git clone http://ID:PW@example.com/git

イメージ的には以下構成となる。

DIR/hoge/.git →http://example.com/hoge/git.git
DIR/fuga/.git →http://example.com/fuga/git.git
cd xxx
git branch -r
git branch 対象ブランチ
git checkout 対象ブランチ
#一度にやる場合は下記
git checkout -b 対象ブランチ
git push --delete origin ブランチ名
git remote -v
origin: レポジトリの場所(URL)の別名
master: ブランチの名前

デフォルトの対象はorigin/masterだ。

git config --list

上記コマンドで今どこを示しているかを確認せよ。

git pull origin ブランチ名称
git remote add NEW_REMOTE git@bitbucket.org:xxxx/foo.git

コマンド版を使う場合の設定

[url "https://"]
        insteadOf = git://
[http]
        sslVerify = false
[user]
        email = username@example.com
        name = username
[remote "origin"]
        url = http://GIT_USER:GIT_PASSWORD@git.example.com/test.git

認証情報の保存

1.git config --global credential.helper store
2.git コマンドを実行してパスワードなどを入力
3.~/.git-credentialsが作成される

SVNとの違い

ローカルリポジトリ、index、リモートリポジトリと構成要素が複雑怪奇。ブランチがSVNより増える傾向がある。

addコミット準備領域であるindexに追加する
commitあくまでローカルリポジトリに対してのコミット。-aオプションをつけないと変更してもコミット対象にはならない
PUSHリモートリポジトリへのコミット。SVNでいうところのコミットと同義。リモートリポジトリがすっからかんの場合は git push -u origin master
FETCHSVNでいうところのアップデート(ローカルへのマージは行わない?)
PULLSVNでいうところのアップデート(ローカルへのマージまで行うFETCH+MERGE)
cloneチェックアウトだが、gitではブランチの切替に利用する
forkgithubの機能。cloneと似ているが、分散開発用に元の所有者に通知がいく。forkするという行為はオリジナルへの貢献を前提とする。
アップストリーム ブランチローカルに対して上流にあるブランチ。cloneした場合はclone元だが、指定もできる
branch -aリモート含むブランチ一覧の表示
branch -dブランチの削除。すっからかんじゃないと削除不可能
git log --pretty=oneline pom.xmlログを省略形の一行で表示。
git log --abbrev-commit pom.xmlコミットIDを省略系。一行で表示。
git log -p ファイル or ディレクトリ指定のファイル or ディレクトリ以下の更新のみ表示

checkout

ローカルブランチを作成して、そこにリモートを紐つけ

git checkout -b local_branch origin/remote_branch

このあとgit fetchが必要。←間違い!git pullと何が違うの?リモートブランチ情報が更新されているかもしれないので事前にfetchしておく!

push

引数なしのpushは非常に危険 http://dqn.sakusakutto.jp/2012/10/git_push.html

ブランチ指定してPUSHgit push --set-upstream origin testing
実際には実行しない(dry-run)git push -n

pull

リモートから取得する(fetch+merge)

git pull origin testingoriginリポジトリのtestingブランチを取り込む

元に戻す系

svnのように簡単ではない。svn revertはgit checkout file名

checkout ファイル名svn revert ファイル名
checkout .すべてのファイルのローカル変更を取消!
revertコミットを取り消す
resetある時点のコミット以降をすべて取り消す!
reset --hardrevertみたいなもの。ローカルコミットは取り消さない

マージ

git checkout merged
git merge マージ元のブランチ(リモートを指定してOK)
git status でUnmergedになっているもの

チェックアウトなど

ファイルの変更サイクル

  1. 変更
  2. add(ステージングにファイルが追加)
  3. commit(ここまではローカルリポジトリ)
  4. push(リモートリポジトリにコミット)

ブランチ運用

masterブランチを統合ブランチとして、リリース向けに運用する。トピックブランチは機能追加、バグ修正のタイミングで作成され、リリース前に統合ブランチにマージされる。 branch -aでリモート含むブランチ状況を確認しながら作業する。

git branch testingtestingブランチ追加
git checkout testingtestingブランチに切替

マージ

first-forwardマージ

分岐以降統合ブランチに変更がない場合、統合ブランチのコミット履歴はトピックブランチのコミット履歴とイコールになる。シンプルなので早送りマージと呼ばれる。ただしトピックブランチの履歴は残らないので、たとえfirst-forwardが可能であってもやらないほうがよい。

マージ

通常のマージ。統合とトピックブランチをマージしたというコミットが作成される。

rebase

ブランチの履歴を消してマージ。トピックブランチが小さいものが多く、頻発するのであればこちらの運用が良いかもしれぬ。

特定のコミットのみマージ

git cherry-pick 5d0b85e02f5ae4c4984fac388015fe5ce1918673
git cherry-pick 1ea6be61498f49f00cfd0fd5a0a08590087930a6

いろいろためした

$ git status
On branch MyBranche
Your branch is up-to-date with 'origin/NewBranch'.
nothing to commit, working directory clean
$ git status
On branch development
Your branch is behind 'origin/Develop' by 6 commits, and can be  fast-forwarded.
 (use "git pull" to update your local branch)
nothing to commit, working directory clean

トラブルシューティング

error: failed to push some refs to 'http://example.com/git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

Your local changes to the following files would be overwritten by checkout:

ブランチ移動時に未コミットのファイルがあると上記エラー。 消してもいいなら以下のコマンドでローカルの変更を戻す。

git checkout .

Install

It's easy to install from source! just configure and make and make install!

apache

git-http-backendがソースコンパイルだとないため、下記はバイナリーインストールのときのみ可能。

http://www.proton.jp/main/programming/git/smart-http.html

SetEnv GIT_PROJECT_ROOT /usr/local/git
SetEnv GIT_HTTP_EXPORT_ALL
 
ScriptAlias /git/ /usr/local/libexec/git-core/git-http-backend/
 
<locationMatch "/git">
   AuthType Basic
   AuthName "Git Area"
   AuthUserFile /usr/local/apache2/htdocs/.htpasswd
   require valid-user
   Order allow,deny
   Allow from all
</locationMatch>

AWSの場合

[root@ip-172-31-17-50 git]# git init
Initialized empty Git repository in /var/www/git/.git/
#Alias /git /var/www/git
SetEnv GIT_PROJECT_ROOT /var/www/git/.git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
<Directory /var/www/git>
  Options +ExecCGI
  AddHandler cgi-script .cgi
  DirectoryIndex gitweb.cgi
</Directory>

gitstatsの使い方

まずはgitをクローンする。そのうえで以下のように実行するとoutput_dirにhtmlが出力される。

gitstats git_clone_dir/ /var/www/html/new_dir
gitstats -c commit_begin=ハッシュ(短いのでもOK)

http://manpages.ubuntu.com/manpages/trusty/man1/gitstats.1.html

文字化け

内部でgnuplotを呼んでいる。フォントがないとarialを使おうとするので、以下のコマンドで回避

yum install vlgothic-fonts.noarch
export GDFONTPATH=/usr/share/fonts/vlgothic
export GNUPLOT_DEFAULT_GDFONT=VL-Gothic-Regular

httpとの連携

http://int128.hatenablog.com/entry/20130118/1358440428

ハマリポイント

リモートと同名のブランチがローカルにも簡単に作れてしまう

既存branchの切り替えにはcheckoutを使うが、間違ってbranchを使うと作成されてしまう。 オペレーションミスだが、警告すら出ないのが問題。一度そうなると修正するのが面倒なのでgit cloneしたほうがよい。

アップストリームとブランチ名の関係が不明瞭

分かりにくい原因がこれに尽きる。サーバーの最新状態をorigin(接続先のエイリアス)/masterであらわされたとするとローカルのmasterはこの時点のスナップショット。リモートと同期するにはgit fetch originを行う。

svnのリポジトリ移行

#ローカルリポジトリをチェックアウト
git svn clone -s https://REPO_URL/
cd xxx
git status

ブランチ間の差分チェック

git diff master:対象ファイル branch:対象ファイル
#!/bin/sh
LEFT=origin/master
RIGHT=origin/branch
DIFFLIST=$1
while read x; do
#  echo $x
  git diff $LEFT:$x $RIGHT:$x >> diff_result.txt
done < $DIFFLIST

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS