2014年5月17日土曜日

Linux(CentOS)でGitを使ってみる

CentOSでGitを使った見た。備忘録。

1.インストール&環境設定

 # yum install git-core

 でインストール。1.7.1がインストールされた。次に設定進む。

 ・ユーザー情報の設定
 # git config --global user.name "Chikayuki Hayashi"
 # git config --global user.email testuser@gmail.com

 ・エディター(vim)、Diffツール(vimdiff)の設定
 # git config --global core.editor vi
 # git config --global merge.tool vimdiff

 設定した内容の確認をしてみる。
 # git config --list
 user.name=Chikayuki Hayashi
 user.email=testuser@gmail.com
 core.editor=vim
 merge.tool=vimdiff
 core.repositoryformatversion=0
 core.filemode=true
 core.bare=false
 core.logallrefupdates=true

 ちなみに上記の設定は
  ~/.gitconfig
 に保存されている

 # cat ~/.gitconfig

 [user]
         name = Chikayuki Hayashi
         email = testuser@gmail.com
 [core]
         editor = vim
 [merge]
         tool = vimdiff

 /etc/gitconfigにも同様に記載することができ、この場合や
  /etc/gitconfig → ~/.gitconfig
 の順で読み込まれ、最後の設定値が優先される。

2.便利な設定

 自動補完

 gitコマンドの補完機能。ソースからcontrib/completion/git-completion.bashをゲットする。

 # wget 'http://git.kernel.org/?p=git/git.git;a=blob_plain;f=contrib/completion/git-completion.bash;hb=HEAD' -O git-completion.bash 
 # mv git-completion.bash ~/.git-completion.bash
  # vi ~/.bashrc
  ===
  source ~/.git-completion.bash
  ===
 # source ~/.bashrc

 エイリアス

 gitコマンドの中でエイリアスを設定することができる。
 たとえばログ出力のフォーマットを変えて見やすくしたい場合は、、

 # git config --global alias.loglist 'log --pretty="%ad --- %h - %s " --date=iso'
 # git loglist
 2014-05-10 07:53:48 +0000 --- 47028f4 - test
 2014-05-10 06:58:07 +0000 --- 2da101f - firsrt commit

 となる(このコマンドのほうが見やすい気がする)。上記の例だと、エイリアスにlogコマンドのオプションもつけることができる

 # git loglist --after=2014-05-11
 2014-05-10 07:53:48 +0000 --- 47028f4 - test
 2014-05-10 06:58:07 +0000 --- 2da101f - firsrt commit

 これは
 # git log --pretty="%ad --- %h - %s " --date=iso --after=2014-05-11
 と一緒。


3.使ってみる(ローカルレポジトリ)

 テストディレクトリを作成し、、

 # mkdir ~/git;cd ~/git

 管理対象となるファイルを適当に作り

 # touch test.txt

 ローカルレポジトリの作成し

 # git init

 作成したファイルをステージングエリアに追加し

 # git add . 

 ローカルレポジトリに登録する

 # git commit -m "first commit"

 次にファイルを変更してみて、

 # echo test > test.txt

 ワーキングツリーとステージングエリア(ローカルレポジトリではない)の内容を差分を見てみる

 # git diff test.txt
 diff --git a/test.txt b/test.txt
 index e69de29..9daeafb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -0,0 +1 @@
 +test

 で、ステージングエリアに登録してみる。

 # git add . 

 この状態でワーキングツリーとステージングエリアの内容を差分を見てみると、、

 # git diff test.txt

 何も差分はない(あたりまえだけど仕組みを確認という意味で)。
 今度は、ステージングエリアとローカルレポジトリの差分を見てみる

 # git diff --cached test.txt
 diff --git a/test.txt b/test.txt
 index e69de29..9daeafb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -0,0 +1 @@
 +test

 でたでた。でローカルレポジトリに登録し、、

 # git commit -m "edit"

 1つ前のコミットと最新のコミットの差分をみると

 # git show HEAD
 commit 913a194ac15b1b4a3e99e0a1ed33f620b3e58604
 Author: Chikayuki Hayashi <testuser@gmail.com>
 Date:   Fri May 16 13:33:20 2014 +0000

     edit

 diff --git a/test.txt b/test.txt
 index e69de29..9daeafb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -0,0 +1 @@
 +test

 こんな感じ。ちなみにさっきのエイリアスに登録したコマンドで見てみると

 # git loglist
 2014-05-16 13:33:20 +0000 --- 913a194 - edit
 2014-05-16 13:08:28 +0000 --- 3b59ca5 - first commit

 次にファイルを再度更新して、コミットする。

 # echo test2 > test.txt
 # git add .
 # git commit -m "third commit"
 # git loglist
 2014-05-16 13:43:50 +0000 --- bfa6970 - third commit
 2014-05-16 13:33:20 +0000 --- 913a194 - edit
 2014-05-16 13:08:28 +0000 --- 3b59ca5 - first commit

 で1回目と3回目(最新)の差分チェックをてみると、

 # git show 3b59ca5..bfa6970
 commit bfa6970d18185e7f78c86ff16506977091459bb9
 Author: Chikayuki Hayashi <testuser@gmail.com>
 Date:   Fri May 16 13:43:50 2014 +0000

     third commit

 diff --git a/test.txt b/test.txt
 index 9daeafb..180cf83 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -1 +1 @@
 -test
 +test2

 commit 913a194ac15b1b4a3e99e0a1ed33f620b3e58604
 Author: Chikayuki Hayashi <testuser@gmail.com>
 Date:   Fri May 16 13:33:20 2014 +0000
 
     edit
 
 diff --git a/test.txt b/test.txt
 index e69de29..9daeafb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -0,0 +1 @@
 +test

 こんな感じで変更経緯がわかる。ヘッダーなどの余計な情報を減らしたい場合は、

 # git show --pretty="%ad --- %h - %s " --date=iso 3b59ca5..bfa6970
 2014-05-16 13:43:50 +0000 --- bfa6970 - third commit

 diff --git a/test.txt b/test.txt
 index 9daeafb..180cf83 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -1 +1 @@
 -test
 +test2
 2014-05-16 13:33:20 +0000 --- 913a194 - edit
 
 diff --git a/test.txt b/test.txt
 index e69de29..9daeafb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -0,0 +1 @@
 +test

 ちょっとはみやすいかな。git logと一緒ですね。

4.使ってみる(リモートレポジトリ)
 
 最後にリモートレポジトリを使ってみる。

 まずssh keyを用意。今回は、以前Windows環境でSourceTreeを試したときに作成した、鍵とgitlabを使う。puttygenで作成したので、opensshに変換したファイルを、今回のCentosの~/.ssh/id_rsaとして保存しておく(パーミッションは600に)。

 次にリモートレポジトリを作成する(gitlab.com上のtestuserアカウントで作成)。

 で、クローンを作るディレクトリに移動して、Clone!ちなみにプロジェクトと同名のディレクトリが無いようしないと、エラーになる。。(※クローンじゃなければ、空じゃなくても大丈夫)

  # cd ~
 # rm -fR test/

 # git clone git@gitlab.com:testuser/test.git

 登録されていたファイルもゲットできました。ちなみに、httpsでアクセスしようとしたら、

 # git clone https://gitlab.com/testuser/test.git
 Initialized empty Git repository in /root/test/.git/
 error: The requested URL returned error: 401 Unauthorized while accessing  https://gitlab.com/testuser/test.git/info/refs

 fatal: HTTP request failed

 エラーになりました。URLにアカウントをつけないといけないらしいです。

 # git clone https://testuser@gitlab.com/testuser/test.git

 これをすると、パスワードが聞かれるので、入れればCloneができる。
 もしくは~/.netrcに情報を入れておくのでも対応できるみたい。
 machine gitlab.com
 login testuser
 password xxxx

 またリモートレポジトリは複数割り当てることができる。たとえば、もう1つtest9というプロジェクトを作成して、このローカルレポジトリに追加すると、、

 # git remote add test9 https://gitlab.com/testuser/test9.git
 # git remote -v
 origin  https://gitlab.com/testuser/test.git (fetch)
 origin  https://gitlab.com/testuser/test.git (push)
 test9   https://gitlab.com/testuser/test9.git (fetch)
 test9   https://gitlab.com/testuser/test9.git (push)

 となる。で、test9にプッシュできる。

 # git push test9 master
 Counting objects: 31, done.
 Compressing objects: 100% (21/21), done.
 Writing objects: 100% (31/31), 20.28 KiB, done.
 Total 31 (delta 9), reused 0 (delta 0)
 To https://gitlab.com/testuser/test9.git
  * [new branch]      master -> master

 とりあえずここまで・・

 ※)手元にあるレポジトリを、リモートレポジトリを作ってPUSHしたい場合は、
 1.リモートレポジトリを作成し、
 2.リモート対象を追加し、
   git remote add xxxx https://gitlab.com/testuser/yyyy.git
 3.上記追加したリモートにpushする
   git push xxxx master
 でいける。

(2018/4/13)備忘録として追記
$ git pull origin master

(gnome-ssh-askpass:12683): Gtk-WARNING **: cannot open display:

というエラーが出た場合、
$ unsetenv SSH_ASKPASS
 or
$ unset SSH_ASKPASS
としたあとに、再度試す。

0 件のコメント:

コメントを投稿