Skip to main content

Using Git restore to discard changes within a file

Posted in Development and Git

I really like git restore, and one of its superpowers is its patch mode, where we can restore parts (or ‘hunks’) of a file, rather than the whole file at once.

Just like git add’s patch mode, we don’t have to use the full --patch flag; we get a handy -p shortcut. The following command would enter patch mode for every file we’ve edited since our last commit:

git restore -p

Alternatively, we can restore parts of a specific file with:

git restore -p path/to/file.html

Once we’re in patch mode, we just need to work our way through the changes by typing a letter from the multitude of options:

Discard this hunk from worktree [y,n,q,a,d,g,/,j,J,k,K,s,e,?]?

In case you’re not sure what any of those mean (a situation I find myself in all the time!), typing ? and hitting enter gives you a nice reference:

y - discard this hunk from worktree
n - do not discard this hunk from worktree
q - quit; do not discard this hunk or any of the remaining ones
a - discard this hunk and all later hunks in the file
d - do not discard this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunkse - manually edit the current hunk
e - manually edit the current hunk
? - print help

Pretty overwhelming… The best place to start is with y (‘yes, discard this hunk’) and n (‘no, don’t discard this hunk’), leaving things like splitting hunks for another day, once you’re more familiar with it all.

Note: git checkout also has a patch mode that does exactly the same as git restore’s patch mode, but the key here is that using the git restore command to restore a file to its previously committed state is both more memorable and semantically correct.

Accessibility in your inbox

I send an accessibility-centric newsletter on the last day of every month, containing:

  • A roundup of the articles I’ve posted
  • A hot pick from my archives
  • Some interesting posts from around the web

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Upgrading from iPhone 13 mini to 16 Pro

    I get a new phone every 3-ish years, give mine to my wife, and now she gives hers to our daughter. I got a 16 Pro this year! Here’s the skinny.

  2. Apple, you’re doing the Dynamic Island wrong

    I love the idea of Dynamic Island; making lemons into lemonade and all that. But, in my opinion, Apple have got the fundamentals mixed up.