Use interactive checkout
The following git-checkout
syntax,
git checkout --patch <tree-ish> -- <paths>
or, equivalently,
git checkout -p <tree-ish> -- <paths>
allows you to interactively check out hunks from one or more files, listed in <paths>
, as recorded in <tree-ish>
(most commonly, a commit).
See the git-checkout
man page for more details.
Example
To fix ideas, here is a toy example (irrelevant stdout is omitted):
# set things up
$ mkdir test
$ cd test
$ git init
# create some content and commit
$ printf "Hello, world.
" > README.md
$ printf "foo
bar
baz
" > test.txt
$ git add .
$ git commit -m "initial commit"
# modify the working tree
$ printf "another line
" >> README.md
$ printf "foo
foo
" > test.txt
# now restore stuff from test.txt as recorded in master's tip
$ git checkout -p master -- test.txt
diff --git b/test.txt a/test.txt
index 0d55bed..86e041d 100644
--- b/test.txt
+++ a/test.txt
@@ -1,2 +1,3 @@
foo
-foo
+bar
+baz
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y
error: patch failed: test.txt:1
error: test.txt: patch does not apply
The selected hunks do not apply to the index!
Apply them to the worktree anyway? y
# Sanity check: inspect the working tree
# (the hunk "bar
baz" from test.txt was restored, as desired)
$ cat test.txt
foo
bar
baz
# (README.md, on the other hand, was not affected, as desired)
$ cat README.md
Hello, world.
another line
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…