Skip to content

Commit 51435bc

Browse files
authored
Merge pull request #1511 from HonkingGoose/create-section-git-restore
2 parents 64c8ace + 2946b8a commit 51435bc

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

book/02-git-basics/sections/undoing.asc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,90 @@ If you would like to keep the changes you've made to that file but still need to
146146
Remember, anything that is _committed_ in Git can almost always be recovered.
147147
Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see <<ch10-git-internals#_data_recovery>> for data recovery).
148148
However, anything you lose that was never committed is likely never to be seen again.
149+
150+
[[undoing_git_restore]]
151+
==== Undoing things with git restore
152+
153+
Git version 2.25.0 introduced a new command: `git restore`.
154+
It's basically a alternative to `git reset` which we just covered.
155+
From Git version 2.25.0 onwards, Git will use `git restore` instead of `git reset` for many undo operations.
156+
157+
Let's retrace our steps, and undo things with `git restore` instead of `git reset`.
158+
159+
===== Unstaging a Staged File with git restore
160+
161+
The next two sections demonstrate how to work with your staging area and working directory changes with `git restore`.
162+
The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them.
163+
For example, let's say you've changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both.
164+
How can you unstage one of the two?
165+
The `git status` command reminds you:
166+
167+
[source,console]
168+
----
169+
$ git add *
170+
$ git status
171+
On branch master
172+
Changes to be committed:
173+
(use "git restore --staged <file>..." to unstage)
174+
modified: CONTRIBUTING.md
175+
renamed: README.md -> README
176+
177+
----
178+
179+
Right below the ``Changes to be committed'' text, it says use `git restore --staged <file>...` to unstage.
180+
So, let's use that advice to unstage the `CONTRIBUTING.md` file:
181+
182+
[source,console]
183+
----
184+
$ git restore --staged CONTRIBUTING.md
185+
$ git status
186+
On branch master
187+
Changes to be committed:
188+
(use "git restore --staged <file>..." to unstage)
189+
renamed: README.md -> README
190+
191+
Changes not staged for commit:
192+
(use "git add <file>..." to update what will be committed)
193+
(use "git restore <file>..." to discard changes in working directory)
194+
modified: CONTRIBUTING.md
195+
196+
----
197+
198+
The `CONTRIBUTING.md` file is modified but once again unstaged.
199+
200+
===== Unmodifying a Modified File with git restore
201+
202+
What if you realize that you don't want to keep your changes to the `CONTRIBUTING.md` file?
203+
How can you easily unmodify it -- revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)?
204+
Luckily, `git status` tells you how to do that, too.
205+
In the last example output, the unstaged area looks like this:
206+
207+
[source,console]
208+
----
209+
Changes not staged for commit:
210+
(use "git add <file>..." to update what will be committed)
211+
(use "git restore <file>..." to discard changes in working directory)
212+
modified: CONTRIBUTING.md
213+
214+
----
215+
216+
It tells you pretty explicitly how to discard the changes you've made.
217+
Let's do what it says:
218+
219+
[source,console]
220+
----
221+
$ git restore CONTRIBUTING.md
222+
$ git status
223+
On branch master
224+
Changes to be committed:
225+
(use "git restore --staged <file>..." to unstage)
226+
renamed: README.md -> README
227+
228+
----
229+
230+
[IMPORTANT]
231+
=====
232+
It's important to understand that `git restore --staged <file>` is a dangerous command.
233+
Any local changes you made to that file are gone -- Git just replaced that file with the most recently-committed version.
234+
Don't ever use this command unless you absolutely know that you don't want those unsaved local changes.
235+
=====

0 commit comments

Comments
 (0)