How to discard selected parts of the local changes?
git checkout -p
To discard only selected parts of your local changes use git checkout -p
.
Git will show parts of your local changes one by one and let you decide what to do with that change.
The available actions for the given hunk are:
y - discard this hunk
n - do not discard this hunk
q - quit discarding hunks
a - discard this and all the remaining hunks in the file
d - do not discard this hunk nor any of the remaining hunks in the file
K - leave this hunk undecided, see previous hunk
g - select a hunk to go to
/ - search for a hunk matching the given regex
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
After selecting an action for the given hunk, git will show the next available hunk. In the example below, the first hunk is discarded with typing y
and the second hunk kept with n
.
git checkout -p
diff --git a/articles_controller.rb b/articles_controller.rb
index ea67e94..99f04b3 100644
--- a/articles_controller.rb
+++ b/articles_controller.rb
@@ -4,7 +4,7 @@ class ArticlesController < ApplicationController
end
def show
- @article = Article.find(params[:id])
+ @article = Article.find(params[:id])
end
def new
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? y
diff --git a/math.rb b/math.rb
index 2021dad..bfd1e54 100644
--- a/math.rb
+++ b/math.rb
@@ -1,14 +1,14 @@
def operation_add(a, b)
- return a - b
+ return a + b
end
def operation_sub(a, b)
- return a - b
+ return a + b
end
def operation_mul(a, b)
- return a * b
+ return a + b
end
def operation_div(a, b)
(1/1) Discard this hunk from worktree [y,n,q,a,d,s,e,?]? n
See git checkout to learn more about it.