setucocms (1.6.1) | 2013-03-24 21:20 |
ここは、SetucoCMSプロジェクトにおける開発に必要なGitの操作方法をまとめたページです。
$ mkdir setucodev $ cd setucodev $ git clone ssh://git@ssh.github.com:443/densetubu/SetucoCMS.git . $ ls SetucoCMS (正しくcloneされていればSetucoCMSのファイルが表示される)
$ git pull
$ git pull origin iss001:iss001
git pullを実行したとき、次のようなエラーが出る場合がある。
CONFLICT (add/add): Merge conflict in xxxxx.xxx Automatic merge failed; fix conflicts and then commit the result.このエラーが表示されたら、自分の編集内容が他のメンバーと競合しているので、該当箇所を手動で編集して、問題を解決しておく。
ローカルリポジトリでファイルを編集中(未コミット)にgit pullを実行し、編集中のファイルがリモートリポジトリ側でも変更されていた場合、
$ git pull *************: needs update *************: needs update *************: needs update ...
$ git pull error: Entry '************' not uptodate. Cannot merge. fatal: merging of trees ************ and *********** failedなどのエラーが出ることがある。この場合、一旦編集中のファイルをコミットするか、git stashで変更を一時退避、またはgit reset --hardで変更を取り消してからgit pullを実行しなおす必要がある。
$ vim index.php # index.php を編集 $ git add index.php
$ git reset HEAD test.txt
$ vim index.php # index.php を編集 $ git diff diff --git a/index.php b/index.php # a〜 は編集前、b〜 は編集後を意味する index 2dad828..01716da 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ <?php -phpinfo(); -echo 123; - +function test () { + echo 123; + phpinfo(); +}
$ vim index.php # index.php を編集 $ git add index.php # index.php を git add $ git diff # 単なる git diff では何も表示されない(git add していない編集内容がないため) $ git diff --cached diff --git a/index.php b/index.php index 2dad828..01716da 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ <?php -phpinfo(); -echo 123; - +function test () { + echo 123; + phpinfo(); +}
※「HEAD」とは最新のcommitを指す。
$ vim index.php # index.php を編集 $ git add index.php # ここまでの編集内容をいったん git add $ vim index.php # 再度index.php を編集 $ git diff --cached diff --git a/index.php b/index.php index e69de29..a73f932 100644 --- a/index.php +++ b/index.php @@ -0,0 +1 @@ +this line is 1st edit # git diff --cached ではgit add された 最初の編集分だけ差分表示される $ git diff diff --git a/index.php b/index.php index e69de29..2ead53d 100644 --- a/index.php +++ b/index.php @@ -0,0 +1,2 @@ this line is 1st edit # git diff では git add された行は「+」も「-」も表示されない +this line is 2nd edit # 2回目の編集で入力した行(まだgit addされていない)だけ差分表示される $ git diff HEAD diff --git a/index.php b/index.php index e69de29..2ead53d 100644 --- a/index.php +++ b/index.php @@ -0,0 +1,2 @@ +this line is 1st edit # 1回目の編集で入力した行(既にgit add済み) +this line is 2nd edit # 2回目の編集で入力した行(まだgit addされていない) 両方が差分表示される
$ git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: index.php # no changes added to commit (use "git add" and/or "git commit -a")
それ以外の部分の意味を、「#コメント」で書くと以下のとおり。
$ git status # On branch master # Changes to be committed: # 編集後にgit addされて、次回のcommit対象になっている # (use "git reset HEAD <file>..." to unstage) # # modified: index.php # # Changed but not updated: # 編集後まだgit addされておらず、commit対象になっていない # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: header.php # # Untracked files: # 作成後まだgit add されておらず、gitの管理対象自体に入っていない # (use "git add <file>..." to include in what will be committed) # # newfile
$ git rm index.php rm 'index.php' $ git status # On branch master # Changes to be committed: # 次回 commit 時に、ファイルの削除が記録されることが分かる # (use "git reset HEAD <file>..." to unstage) # # deleted: index.php #
$ rm index.php $ git status # On branch master # Changed but not updated: # 次回 commit 時の対象になっていない # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: index.php # no changes added to commit (use "git add" and/or "git commit -a") $ git rm index.php rm 'index.php' $ git status # On branch master # Changes to be committed: # 次回 commit 時の記録対象になった # (use "git reset HEAD <file>..." to unstage) # # deleted: index.php #
$ git mv index.php page.php $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: index.php -> page.php #
$ git commit
1. コミットの理由と内容(1行かつ50文字以内。) 2. 3. 詳細なコミットの内容や変更した理由(行数、文字数の制限はない。) 4. 〜(詳細な記述の続き)〜 5. 〜(さらに続き)〜・・・
1行目だけで理由と編集内容が表現しきれれば3行目以降は不要だが、できるだけ面倒くさがらずに「なぜ」「何をしたか」を書くこと。
各ファイルの内容はコミットしたときのまま、「コミットしたこと」だけを取り消すことができる。
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: index.php # $ git commit # 誤ってコミットしてしまった $ git reset --soft HEAD^ # コミットだけを取り消す index.php: locally modified $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: index.php # git commit前、git add済みの状態に戻る #上の例では、index.phpを編集した後に誤ってコミットしてしまったが、コミットだけを取り消し、index.phpは引き続き編集することができるようになっている。
なお、「--soft」を省略しても、同様に編集内容を残してコミットだけを取り消すことができるが、その場合は「git add」もされていない状態に戻る。
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: index.php # $ git commit # 誤ってコミットしてしまった $ git reset HEAD^ # コミットだけを取り消す index.php: locally modified $ git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: index.php # git commit前、git add前の状態に戻る #
$ git revert HEAD
1. Revert "前回のコミット要約" 2. 3. This reverts commit 前回のコミットハッシュ値 4. 5. # Please enter ...
1. Revert "前回のコミット要約" 2. 3. This reverts commit 前回のコミットハッシュ値 4. index.phpを間違えてコミットしてしまったため、git revert実行。 5. 誤)<?= $path ?> 正)<?php echo $path ?> 7. この後、正のように編集しなおしてコミットしなおす予定。 8. # Please enter ...
$ git reset --hard
みんなで使っているmasterブランチには変更を加えず、実験的にソースコードに変更を加えられる。
$ git branch sample
$ git branch * master sample*「*」がついているmasterブランチが現在の作業場所
$ git checkout sample $ git branch master * sample
sampleブランチに移動した。
$ git checkout master # まず、反映先のブランチ(=master)に移動 $ git branch # 現在のブランチの状況を確認 * master sample $ git merge sample # sampleブランチをmasterブランチにマージ
sampleブランチで行った編集内容が反映されていることを確かめておく。
$ git branch -d sample
$ git glog * xxxxxxx <AkihiroTSUKADA> - (HEAD, master) Revert "index.phpにコメントを追加" (6 minutes ago) * yyyyyyyy <AkihiroTSUKADA> - index.phpにコメントを追加 (46 minutes ago) * zzzzzzz <AkihiroTSUKADA> - index.phpを作成 (58 minutes ago)
(git glogコマンドについてはGit導入〜各種設定>エイリアス(コマンドのショートカット)の登録 を参照)
$ git checkout zzzzzzz
Note: checking out 'zzzzzzz'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at zzzzzzz... index.phpを作成
$ git branch * (no branch) master
$ git checkout master
これで、任意の時点のディレクトリ内容を確認し、最新の時点に再度戻ってくることができる。
$ git checkout HEAD -- test_file.txt
text_file.txtが最後にコミットした状態に置き換わる
$ git push origin master
$ git push origin master To ssh://git@ssh.github.com:443/densetubu/SetucoCMS.git ! [rejected] master-> master(non-fast forward) error: failed to push some refs to 'ssh://git@ssh.github.com:443/densetubu/SetucoCMS.git'
$ git pull $ git push origin master
このときgit pullでエラーが出やすいが、そのときはgit pull時のエラーを参照のこと。
[PageInfo]
LastUpdate: 2012-06-25 17:01:44, ModifiedBy: mars-3
[License]
Creative Commons 2.1 Attribution-ShareAlike
[Permissions]
view:all, edit:members, delete/config:members