Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Git | git reset で各種取り消し操作

概要

git reset で各種取り消し操作

詳細

git reset で各種取り消し操作が可能です。

--soft オプション

ワーキングツリーとインデックスはそのままでHEADの位置を変更します。

オペレーション

  • hoge.txtを追加、ステージング、コミット
  • hige.txtを追加、ステージング、コミット
  • soft リセット
$ git init
Initialized empty Git repository in %some_place%/.git/
$ echo hoge> hoge.txt
$ git add -A
$ git commit -m "add hoge.txt"
[master (root-commit) 0e5882c] add hoge.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hoge.txt
$ echo hige>hige.txt
$ git add hige.txt
$ git commit -m "invalid commit"
$ git reset --soft HEAD^
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   hige.txt
#

# hige.txt はステージングされたまま

$ ls
hige.txt  hoge.txt

# hige.txt は残っている

※HEAD^はひとつ前のコミット

--hard オプション

ワーキングツリー・インデックス・HEADすべてを移動

オペレーション

  • hoge.txtを追加、ステージング、コミット
  • hige.txtを追加、ステージング、コミット
  • hard リセット
$ git init
Initialized empty Git repository in %some_place%/.git/
$ echo hoge > hoge.txt
$ git add -A
$ git commit -m "add hoge.txt"
[master (root-commit) 6ef034c] add hoge.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hoge.txt
$ echo hige>hige.txt
$ git add hige.txt
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   hige.txt
#
$ git commit -m "add hige.txt"
[master 098def3] add hige.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hige.txt
$ git log
commit 098def32d0345cf4509a3a21785adb9dcba34943
Author: <ユーザー名>
Date:   Tue Jun 17 23:23:14 2014 +0900

    add hige.txt

commit 6ef034cc6aa80458299c76571dd2d47e2a2234f9
Author: <ユーザー名>
Date:   Tue Jun 17 23:22:38 2014 +0900

    add hoge.txt
$ git reset --hard HEAD^
HEAD is now at 6ef034c add hoge.txt
$ ls
hoge.txt

# hige.txt がなくなった

$ git status
# On branch master
nothing to commit (working directory clean)

# インデックスに hige.txt がなくなった

$ git log
commit 6ef034cc6aa80458299c76571dd2d47e2a2234f9
Author: <ユーザー名>
Date:   Tue Jun 17 23:22:38 2014 +0900

    add hoge.txt

# 履歴に hige.txt がなくなった
--mixed オプション

ワークツリーはそのままでインデックスの変更を取消します。

    • mixed オプションは git reset コマンドのデフォルト値です。

オペレーション

  • hoge.txtを追加、ステージング、コミット
  • hige.txtを追加、ステージング、コミット
  • mixed リセット
$ git init
Initialized empty Git repository in %some_place%/.git/
$ echo hoge > hoge.txt
$ git add -A
$ git commit -m "add hoge.txt"
[master (root-commit) ec7a8cf] add hoge.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hoge.txt
$ echo hige > hige.txt
$ git add -A
$ git commit -m "add hige.txt"
[master 490a6bc] add hige.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hige.txt
$ git log
commit 490a6bcf99c7f827058baa89c105d31cc4e7c408
Author: <ユーザー名>
Date:   Tue Jun 17 23:03:28 2014 +0900

    add hige.txt

commit ec7a8cf74d873f390cdfe99fc562bd85cf637855
Author: <ユーザー名>
Date:   Tue Jun 17 23:03:15 2014 +0900

    add hoge.txt
$ git reset --mixed HEAD^
$ ls
hige.txt  hoge.txt
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       hige.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git log
commit ec7a8cf74d873f390cdfe99fc562bd85cf637855
Author: <ユーザー名>
Date:   Tue Jun 17 23:03:15 2014 +0900

    add hoge.txt