Partially restoring a saved file to the last committed version

In a nutshell: Use git checkout -p to restore parts of your file to the committed version.

Problem

  • You saved some changes to a file notes that you want to commit.
  • You also made some changes to notes that you want to discard.
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   notes

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

If you’ve already called git add, you will have to tell Git not to mark these changes for commit first.

Solution

git checkout has a feature called patch mode, which allows you to review unstaged changes. For every change, Git will ask you whether you want to keep it or discard it.

$ git checkout -p
diff --git a/notes b/notes
index da223ae..d0e8597 100644
--- a/notes
+++ b/notes
@@ -1,3 +1,7 @@
 Woke vinyl lyft bushwick taxidermy synth, mumblecore YOLO ethical small batch. Swag williamsburg waistcoat vexillologist. Glossier beard gastropub offal. Celiac humblebrag twee truffaut readymade, tumeric enamel pin fixie hashtag kickstarter keytar sriracha. Kitsch mustache chambray, shabby chic crucifix migas drinking vinegar poutine street art cray mumblecore williamsburg tousled master cleanse. Chia polaroid kickstarter fingerstache. Coloring book fingerstache roof party trust fund bushwick.

+I want to keep this new paragraph.
+
 Scenester portland polaroid glossier food truck. Intelligentsia hella banjo disrupt irony vinyl. Normcore narwhal fixie, four loko hella banh mi umami af tofu. Shoreditch succulents direct trade pug brunch, disrupt lo-fi. Live-edge tofu street art ugh, four dollar toast beard pour-over hella cronut single-origin coffee 3 wolf moon kinfolk meditation. La croix butcher fixie, chia humblebrag kitsch tilde sartorial irony chillwave asymmetrical single-origin coffee. Iceland actually before they sold out, kogi microdosing brunch DIY 3 wolf moon cred.
+
+I want to discard this new paragraph.
Discard this hunk from worktree [y,n,q,a,d,/,s,e,?]?

Because we want to keep the first new paragraph and discard the second one, we’ll have to split this hunk first. As the help function underneath the ? key will tell you, we can enter s to split it.

Split into 2 hunks.
@@ -1,3 +1,5 @@
 Woke vinyl lyft bushwick taxidermy synth, mumblecore YOLO ethical small batch. Swag williamsburg waistcoat vexillologist. Glossier beard gastropub offal. Celiac humblebrag twee truffaut readymade, tumeric enamel pin fixie hashtag kickstarter keytar sriracha. Kitsch mustache chambray, shabby chic crucifix migas drinking vinegar poutine street art cray mumblecore williamsburg tousled master cleanse. Chia polaroid kickstarter fingerstache. Coloring book fingerstache roof party trust fund bushwick.

+I want to keep this new paragraph.
+
 Scenester portland polaroid glossier food truck. Intelligentsia hella banjo disrupt irony vinyl. Normcore narwhal fixie, four loko hella banh mi umami af tofu. Shoreditch succulents direct trade pug brunch, disrupt lo-fi. Live-edge tofu street art ugh, four dollar toast beard pour-over hella cronut single-origin coffee 3 wolf moon kinfolk meditation. La croix butcher fixie, chia humblebrag kitsch tilde sartorial irony chillwave asymmetrical single-origin coffee. Iceland actually before they sold out, kogi microdosing brunch DIY 3 wolf moon cred.
Discard this hunk from worktree [y,n,q,a,d,/,j,J,g,e,?]?

We do not want to discard this hunk, so enter n. Next question:

@@ -3 +5,3 @@
 Scenester portland polaroid glossier food truck. Intelligentsia hella banjo disrupt irony vinyl. Normcore narwhal fixie, four loko hella banh mi umami af tofu. Shoreditch succulents direct trade pug brunch, disrupt lo-fi. Live-edge tofu street art ugh, four dollar toast beard pour-over hella cronut single-origin coffee 3 wolf moon kinfolk meditation. La croix butcher fixie, chia humblebrag kitsch tilde sartorial irony chillwave asymmetrical single-origin coffee. Iceland actually before they sold out, kogi microdosing brunch DIY 3 wolf moon cred.
+
+I want to discard this new paragraph.
Discard this hunk from worktree [y,n,q,a,d,/,K,g,e,?]?

This is the one we wanted to get rid of, so enter y. Git is done asking questions at this point. The file now looks like this in an editor:

Woke vinyl lyft bushwick taxidermy synth, mumblecore YOLO ethical small batch. Swag williamsburg waistcoat vexillologist. Glossier beard gastropub offal. Celiac humblebrag twee truffaut readymade, tumeric enamel pin fixie hashtag kickstarter keytar sriracha. Kitsch mustache chambray, shabby chic crucifix migas drinking vinegar poutine street art cray mumblecore williamsburg tousled master cleanse. Chia polaroid kickstarter fingerstache. Coloring book fingerstache roof party trust fund bushwick.

I want to keep this new paragraph.

Scenester portland polaroid glossier food truck. Intelligentsia hella banjo disrupt irony vinyl. Normcore narwhal fixie, four loko hella banh mi umami af tofu. Shoreditch succulents direct trade pug brunch, disrupt lo-fi. Live-edge tofu street art ugh, four dollar toast beard pour-over hella cronut single-origin coffee 3 wolf moon kinfolk meditation. La croix butcher fixie, chia humblebrag kitsch tilde sartorial irony chillwave asymmetrical single-origin coffee. Iceland actually before they sold out, kogi microdosing brunch DIY 3 wolf moon cred.

Special thanks

Credit goes to hipsum.co for the hipster-like placeholder text.