Gitでコミットしてみる
Gitでコミットしてみる。
現在の状態
前回の作成したリポジトリの中身を少し変更して、
現状、sample1.txtとsample2.txtの2ファイルがある状態。
$ ls
README.md sample1.txt sample2.txt
$ git status # On branch master nothing to commit (working directory clean)
修正する
ファイルをいくつか修正する。
・sample1.txtを変更
・sample2.txtを削除
・sample3.txtを作成
$ vi sample1.txt $ rm sample2.txt $ touch sample3.txt
現在の状態は下記の通り。
$ ls
README.md sample1.txt sample3.txt
$ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: sample1.txt # deleted: sample2.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # sample3.txt no changes added to commit (use "git add" and/or "git commit -a")
インデックスに追加する
修正をインデックスに反映する。
$ git add -A
現在の状態は下記の通り。
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # deleted: sample2.txt # new file: sample3.txt #
コミットする
git commit [-m "コミットメッセージ"]
でコミットできる。
$ git commit -m "ファイルの変更と削除と作成" [master a2a5e50] ファイルの変更と削除と作成 3 files changed, 2 insertions(+), 1 deletions(-) delete mode 100644 sample2.txt create mode 100644 sample3.txt
ログでコミットされているのが確認できる。
$ git log commit a2a5e506d0e0a478e6c325979348a9116ac41c10 Author: Your Name <you@example.com> Date: Mon Mar 3 07:35:01 2014 +0900 ファイルの変更と削除と作成
コミットを取り消す
git reset --soft HEAD^
でコミットを取り消すことができる。
$ git reset --soft HEAD^
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: sample1.txt # deleted: sample2.txt # new file: sample3.txt #
ログからも最後にコミットした内容が無くなっている。
$ git log
コミットを取り消し、ワークツリーまでも元に戻す
一旦、再度コミットする。
$ git commit -m "再度コミット" [master 2ad4091] 再度コミット 3 files changed, 2 insertions(+), 1 deletions(-) delete mode 100644 sample2.txt create mode 100644 sample3.txt
$ git log commit 2ad4091e255d08f247b90787a7e5671851554845 Author: Your Name <you@example.com> Date: Mon Mar 3 07:40:07 2014 +0900 再度コミット
git reset --hard HEAD^
でコミットの取り消しだけでなくワークツリーまでも元に戻る。
$ git reset --hard HEAD^ HEAD is now at 0abacf9 test
修正が元に戻ってるのが確認できる。
$ ls
README.md sample1.txt sample2.txt
$ git status # On branch master nothing to commit (working directory clean)
ログからも最後にコミットした内容が無くなっている。
$ git log
インデックスの追加とコミットをまとめて行う
あらためてファイルを修正する
$ vi sample1.txt $ rm sample2.txt $ touch sample3.txt
git commit -a [-m "コミットメッセージ"]
「-a」を付けてコミットするとaddからcommitまでまとめて行ってくれる
$ git commit -a -m "改めてファイルを変更、削除、作成" [master a615123] 改めてファイルを変更、削除、作成 2 files changed, 1 insertions(+), 1 deletions(-) delete mode 100644 sample2.txt
ただし、新規に作成されたsample3.txtはコミットされず、
変更または削除したファイルのみコミットされる。
$ git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # sample3.txt nothing added to commit but untracked files present (use "git add" to track)
コミットの修正
git commit --amend
「--amend」をつけると、直前のコミットを上書きできる。
$ git add sample3.txt
$ git commit --amend [master 9eb3566] 改めてファイルを変更、削除、作成 3 files changed, 2 insertions(+), 1 deletions(-) delete mode 100644 sample2.txt create mode 100644 sample3.txt
ログは増えていない。
$ git log commit 9eb35662d76ce6525c82eefa890e94be21020bad Author: Your Name <you@example.com> Date: Mon Mar 3 08:03:42 2014 +0900 改めてファイルを変更、削除、作成
$ git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # nothing to commit (working directory clean)
HEADのログ
get reflog
を使うと、HEADの履歴を確認できる。
$ git reflog 9eb3566 HEAD@{0}: commit (amend): 改めてファイルを変更、削除、作成 a615123 HEAD@{1}: commit: 改めてファイルを変更、削除、作成 0abacf9 HEAD@{2}: HEAD^: updating HEAD 2ad4091 HEAD@{3}: commit: 再度コミット 0abacf9 HEAD@{4}: HEAD^: updating HEAD a2a5e50 HEAD@{5}: commit: ファイルの変更と削除と作成 ・・・