Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one interactivley discard modifications (similarly to `git add -p`)

Tags:

git

How can one discard modifications from tracked files similarly to what git add -p does with the index? That is I have modified a tracked file and want to undo only parts of the file?

like image 667
Micha Wiedenmann Avatar asked Feb 01 '26 20:02

Micha Wiedenmann


1 Answers

One can use

git checkout -p

to undo parts of a tracked file (see git checkout):

you can use git checkout -p to selectively discard edits from your current working tree.

Example Output

$ git checkout -p
diff --git a/foo.txt b/foo.txt
index 98d8da3..dfd6895 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1 @@
-Hello world
+Hello world!
Discard this hunk from worktree [y,n,q,a,d,/,e,?]?
like image 126
Micha Wiedenmann Avatar answered Feb 03 '26 10:02

Micha Wiedenmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!