Git:git restoreコマンドで対話的(インタラクティブ)に元に戻す方法

スポンサーリンク

git restoreコマンドで対話的(インタラクティブ)に元に戻す方法

git restoreコマンドで対話的(インタラクティブ)に元に戻すには-pオプションを指定します。

 

オプションの説明

 -p, --patch
     Interactively select hunks in the difference between the restore source and the restore location. See the “Interactive Mode” section of git-add(1) to
     learn how to operate the --patch mode.

     Note that --patch can accept no pathspec and will prompt to restore all modified paths.

 

-pオプションを指定した場合

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.md
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git restore -p  # ?でヘルプを表示、yで元に戻す、nで残したまま
diff --git a/test.md b/test.md
index e69de29..9daeafb 100644
--- a/test.md
+++ b/test.md
@@ -0,0 +1 @@
+test
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? ?
y - discard this hunk from worktree
n - do not discard this hunk from worktree
q - quit; do not discard this hunk or any of the remaining ones
a - discard this hunk and all later hunks in the file
d - do not discard this hunk or any of the later hunks in the file
e - manually edit the current hunk
? - print help
@@ -0,0 +1 @@
+test
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? y

diff --git a/test.txt b/test.txt
index de98044..9405325 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,5 @@
 a
 b
 c
+d
+e
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? n

$ git status  # test.mdだけ元に戻っている
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")