![Forrest logo](/images/logo.png)
git-restore:tldr:27609
The git restore --patch
command is used to selectively revert or discard changes to your code files or directories, interactively applying the modifications. This command is available from Git version 2.23 and onward.
Here's how the command works:
-
git restore
: This is the main command used to interact with Git's restore functionality. It is used to restore files or directories to a previous state. -
--patch
: This flag allows you to selectively choose different chunks of changes within a file to be restored individually. It initiates an interactive mode where you can decide which modifications to restore or discard.
When you use git restore --patch
, Git presents you with a diff of the changes you have made in your working tree. The diff shows the differences between the current state and the last commit. You will see each modification chunk, and Git will prompt you to choose one of the following options:
y
: Restores the selected changes chunk.n
: Keeps the selected changes chunk.q
: Quits and does not apply any more changes.a
: Applies the selected action to all remaining changes chunks.d
: Skips the selected changes chunk, but does apply the following changes.
By selecting the appropriate option for each modifications chunk, you can selectively revert or discard changes to your code. Once you have made your choices, Git will update the working tree accordingly.
It's important to note that the git restore
command is primarily used for undoing modifications to your working tree and local changes. If you want to undo commits that have been pushed to a remote repository, you should use other commands like git revert
or git reset
depending on your specific requirements.