mirror of
https://github.com/jbranchaud/til
synced 2026-03-05 23:48:46 +00:00
Add Undo Latest Changes Committed To Specific File as a Git TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
36
git/undo-latest-changes-committed-to-specific-file.md
Normal file
36
git/undo-latest-changes-committed-to-specific-file.md
Normal file
@@ -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.
|
||||
Reference in New Issue
Block a user