2013年1月5日土曜日

git との格闘w その2 いろいろ変更

さて、サーバーにも接続できるようになったところでチュートリアルに戻ります。さっき clone したところに移動して、
$ vi README        <-- 適当に編集
$ git commit -a
[master 2698c25] Add one line
 1 files changed, 1 insertions(+), 0 deletions(-)
git commit をするとエディタが上がるので適当に commit のコメントを追加。これも git push origin master で push してみるとうまくいく。なるほど。

次はstaging。
$ vi README 
$ vi tmp/README 
$ git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
# modified:   README
# modified:   tmp/README
#
no changes added to commit (use "git add" and/or "git commit -a")
何か変更を作ってみた。一つ staging。
$ git add README 
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# modified:   README
#
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
# modified:   tmp/README
#
コミットしてみる。
$ git commit
[master ae419f9] Add another line
 1 files changed, 1 insertions(+), 0 deletions(-)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
# modified:   tmp/README
#
no changes added to commit (use "git add" and/or "git commit -a")
ほほう。1つはちゃんと commit されて、もう一つの変更はそのままと。さっき何も考えずに commit -a としていたが、その -a は全部ファイルを commit しちゃうためのものだったとか。(それを知らずに -a して全部コミットされたアカウントはこちらですw その場合は git reset --hard HEAD~ とやると全部投げ捨てられる模様。)なるほどなるほど。この状態でまた push すると当然 commit されたものだけ飛んでいきます。

次はいよいよブランチ。
$ git branch
* master
作成。
$ git branch experiment
$ git branch
  experiment
* master
作成されました。
$ git checkout experiment
M tmp/README
Switched to branch 'experiment'
$ git branch
* experiment
  master
$ git status
# On branch experiment
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
# modified:   tmp/README
#
no changes added to commit (use "git add" and/or "git commit -a")
なるほど・・・。さっき commit してなかった変更とかはそのままなんですね。当たり前か。
$ vi TODO
$ git add TODO 
$ git commit -am "Commit on branch"
[experiment 700f0dc] Commit on branch
 2 files changed, 3 insertions(+), 1 deletions(-)
 create mode 100644 TODO
$ git status
# On branch experiment
nothing to commit (working directory clean)
あれ、commit どこいった?
$ git diff master
diff --git a/TODO b/TODO
new file mode 100644
index 0000000..295f16a
--- /dev/null
+++ b/TODO
@@ -0,0 +1,2 @@
+Todo file
+
diff --git a/tmp/README b/tmp/README
index edb0724..3105724 100644
--- a/tmp/README
+++ b/tmp/README
@@ -1,2 +1,2 @@
 This is sample file 2.
-
+This is another line in another file.
とりあえず master との比較ではずれがあることはわかる。(コマンドがこれでいいのかは不明。)
$ ls
README  tmp  TODO
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
$ ls 
README  tmp

さあ conflict させてみますよ。
$ vi tmp/README 
$ git commit -am 'Conflict!'
[master 9fb2424] Conflict!
 1 files changed, 1 insertions(+), 1 deletions(-)
これで master と experiment がそれぞれ先へ進みました。マージしてみます。
$ git merge experiment
Auto-merging tmp/README
CONFLICT (content): Merge conflict in tmp/README
Automatic merge failed; fix conflicts and then commit the result.
おうふ・・・。ちょっと変更が近すぎましたかね。TODO のほうは成功したんですが tmp/README がだめでした。ファイルの中身はどうなってるかな?
$ cat tmp/README 
<<<<<<< HEAD
This is sample file 2 and more!.

=======
This is sample file 2.
This is another line in another file.
>>>>>>> experiment
解決するには編集して普通に add して commit だそうで。
$ vi tmp/README 
$ git add tmp/README 
$ git commit -am 'Resolve conflict'
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
nothing to commit (working directory clean)
それぞれの変更が入った状態の完成です。最後にワークに使った branch を消す。
$ git branch -d experiment
Deleted branch experiment (was 700f0dc).

0 件のコメント:

コメントを投稿