删除行时如何编辑补丁结帐?

时间:2016-04-04 15:35:14

标签: git bash patch

我有一个文件bar.txt,其中我删除了3行,但尚未播放任何内容。我想通过git checkout -p恢复其中一条删除的行。

示例/ Bash输出:

$ git checkout -p foo/bar.txt
diff --git a/bin/custom/foo/bar.txt b/bin/custom/foo/bar.txt
index 35483b7..e945cae 100644
--- a/bin/custom/foo/bar.txt
+++ b/bin/custom/foo/bar.txt
@@ -1097,9 +1097,6 @@ mond.desc.refinements   = Foo
 mond.desc.removeAttribute                    = Foo
 mond.desc.resultsForStore                    = Foo
 mond.no.results                              = Foo
-mond.page.CO.class.name                      = Foo
-mond.page.CO.parent.name                     = Foo
-mond.page.brands.parent.name                 = Foo
 mond.page.cmscontent.datetime                = Foo
 mond.page.currentPage                        = Foo
 mond.page.firstPage                          = Foo

我尝试过这样的事情:

@@ -1097,9 +1097,8 @@ mond.desc.refinements   = Foo
 mond.desc.removeAttribute                    = Foo
 mond.desc.resultsForStore                    = Foo
 mond.no.results                              = Foo
-mond.page.CO.class.name                      = Foo
 mond.page.CO.parent.name                     = Foo
 mond.page.brands.parent.name                 = Foo
 mond.page.cmscontent.datetime                = Foo
 mond.page.currentPage                        = Foo
 mond.page.firstPage                          = Foo

但它不起作用。

1 个答案:

答案 0 :(得分:3)

I'd probably go about this in the other direction. It's less mind twisting to git add -p the changes you want, commit them, and then git checkout . to clear the other stuff. If you don't actually want to commit yet, just git reset HEAD^ to undo the commit we just made, leaving the kept changes in your workspace.

But that's not what you asked, and not nearly as fun, so...


This is confusing in the way double negatives aren't not confusing, but you can manage it if you bend your brain right.

Let's say we have the file numbers, containing:

one
two
three
four

We delete two and three, so git diff looks like:

diff --git a/numbers b/numbers
index f384549..a9c7698 100644
--- a/numbers
+++ b/numbers
@@ -1,4 +1,2 @@
 one
-two
-three
 four

Now we run git checkout -p numbers. Notice the prompt asks if you want to discard the hunk that subtracts two and three. If you say y, it discards the subtraction, leaving you with a complete file including all four numbers.

 one
-two
-three
 four
Discard this hunk from worktree [y,n,q,a,d,/,e,?]?

So now we'll edit the hunk to create the hunk we want to discard. That means if we actually want to delete only two, we want to discard the change that deleted three. Counter to intuition, that means you edit the hunk by deleting -two.

@@ -1,4 +1,2 @@
 one
-three
 four

This discards the removal of three, and does not discard the removal of two, leaving us with the diff:

diff --git a/numbers b/numbers
index f384549..ceb5927 100644
--- a/numbers
+++ b/numbers
@@ -1,4 +1,3 @@
 one
-two
 three
 four

So to summarize, delete the lines from the hunk that you still want deleted. Leave the subtraction lines you want to discard. Your edited hunk will contain subtractions (to be discarded) for the lines you're keeping and will not contain the lines you still want to remove.