From e2603f14455008f8331c7ad24be3caef961160e8 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 5 Mar 2026 14:53:55 -0600 Subject: [PATCH] Add Undo Latest Changes Committed To Specific File as a Git TIL --- README.md | 3 +- ...test-changes-committed-to-specific-file.md | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 git/undo-latest-changes-committed-to-specific-file.md diff --git a/README.md b/README.md index c165851..91dd465 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1751 TILs and counting..._ +_1752 TILs and counting..._ See some of the other learning resources I work on: @@ -445,6 +445,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Transition A Branch From One Base To Another](git/transition-a-branch-from-one-base-to-another.md) - [Turn Off The Output Pager For One Command](git/turn-off-the-output-pager-for-one-command.md) - [Two Kinds Of Dotted Range Notation](git/two-kinds-of-dotted-range-notation.md) +- [Undo Latest Changes Committed To Specific File](git/undo-latest-changes-committed-to-specific-file.md) - [Unstage Changes Wih Git Restore](git/unstage-changes-with-git-restore.md) - [Untrack A Directory Of Files Without Deleting](git/untrack-a-directory-of-files-without-deleting.md) - [Untrack A File Without Deleting It](git/untrack-a-file-without-deleting-it.md) diff --git a/git/undo-latest-changes-committed-to-specific-file.md b/git/undo-latest-changes-committed-to-specific-file.md new file mode 100644 index 0000000..2510a4f --- /dev/null +++ b/git/undo-latest-changes-committed-to-specific-file.md @@ -0,0 +1,36 @@ +# Undo Latest Changes Committed To Specific File + +I'm reviewing the changes I've made in a PR before I request a review from my +team. There are a scattering of changes in one file that I've changed my mind +on. Everything else looks good though. So, I need to undo the changes in that +file before proceeding. + +Manually undoing them is going to be clunky. There is a way to do it with `git +checkout`, but that is one of the ways in which `git-checkout` was overloaded +leading to the release of `git-restore`. + +Let's use `git-restore` instead. By specifying a `--source`, I can tell `git` +what _ref_ in the commit history that file should be restored to. I'm on a +short-lived feature branch, so pointing to `main` is good enough. + +```bash +$ git restore --source=main app/models/customer.rb +``` + +If I've changed a file at multiple points on this feature branch and I don't +want to undo all of them, then pointing to `main` is no longer going to work. +Instead, I can point to the commit right before the current one (`HEAD`) that +I'm trying to undo. + +```bash +$ git restore --source=HEAD~ app/models/customer.rb +``` + +This really isn't much different than the `git-checkout` version, but I still +find it to be a little clearer. + +```bash +$ git checkout HEAD~ -- app/models/customer.rb +``` + +See `man git-restore` for more details.