日本語

How to Undo Deleted Files and Changes with git restore

​​Git​command line

If you deleted a file by mistake, or edited a file and no longer want the changes, you can restore it to its state at the last commit with git restore. To unstage a git add, or to take a file from the commit before the latest one, you use the same git restore command with an option. I checked what each operation restores by looking at git status and the file contents in a test environment.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and set the commit dates with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. git restore was added in Git 2.23.0, so it does not exist in older versions of Git. I have not tested on Windows or macOS.

Where you are restoring from and to

To tell the git restore variants apart, it helps to keep in mind the three places where a file's content lives: the latest commit (HEAD), the stage (index) that holds what you added with git add, and the working tree where you edit files.

Three boxes: the latest commit, the stage, and the working tree. git restore --staged copies from HEAD to the stage, git restore copies from the stage to the working tree, and git restore --staged --worktree copies from HEAD to both
Options of git restore and where each one restores from and to

According to the official git restore reference, without options it restores the working tree, with --staged it restores only the stage, and with both options it restores both. The source it restores from is HEAD when --staged is given, and the stage otherwise. In other words, a plain git restore resets the working tree to the content of the stage, not to the last commit. This difference shows up in the result in the case described later, where you added a file and then edited it again.

The examples below were run in a repository with three files: README.md, price.txt, and news.txt. In the last commit, the shipping fee in price.txt is 400 yen.

console
$ git log --oneline
64db4ca 送料を400円に変更
54cce3b 最初のコミット

Restore an edited file to the state of the last commit

This is the case where you changed the shipping fee in price.txt to 0 yen and then realized the change was not needed. git status shows how to restore the file.

console
$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   price.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git diff
diff --git a/price.txt b/price.txt
index ea0fb19..eb91650 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
-送料: 400円
+送料: 0円
 手数料: 200円

Pass the file name to git restore, as shown in the output. It finishes with no output, but the file has been restored.

console
$ git restore price.txt

$ git status
On branch main
nothing to commit, working tree clean

$ cat price.txt
送料: 400円
手数料: 200円
Warning

Running git restore discards your local changes to that file. The Undoing Things section of Pro Git (English edition) describes git restore <file> as a dangerous command and recommends using it only when you are sure you do not need the changes. The same section also says that once you lose work that was never committed, it is likely gone for good.

Restore a deleted file

If you delete news.txt with rm, you can restore it with the same command. git status shows it as deleted:.

console
$ rm news.txt

$ git status
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    news.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git restore news.txt

$ ls
README.md
news.txt
price.txt

$ cat news.txt
営業時間: 10時から18時

If you deleted with git rm, restore with –staged first

If you delete a file with git rm instead of rm, it is removed from the stage too. In this state, running git restore news.txt gave an error.

console
$ git rm -q news.txt

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    news.txt


$ git restore news.txt
error: pathspec 'news.txt' did not match any file(s) known to git

A plain git restore restores from the stage to the working tree. Since news.txt has been removed from the stage, there is nothing to restore from. Restoring to the stage first with --staged, and then to the working tree, brought the file back.

console
$ git restore --staged news.txt

$ git status
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    news.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git restore news.txt

$ git status
On branch main
nothing to commit, working tree clean

Undo a git add

If you have run git add but do not want to include the change in a commit yet, add --staged. Here, I added the change that set the handling fee to 0 yen, and then undid it.

console
$ git add price.txt

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   price.txt


$ git restore --staged price.txt

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   price.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ cat price.txt
送料: 400円
手数料: 0円

price.txt moved from "changes to be committed" to "changes not yet added". The file content (handling fee: 0 yen) is unchanged. --staged restores only the stage and does not touch the working tree.

The same applies when you git add a new file that has never been committed: --staged undoes the add. In the test environment, the file was not deleted. It returned to the state of a file that Git does not yet track (??).

console
$ git add memo.txt

$ git restore --staged memo.txt

$ git status --short
?? memo.txt

$ cat memo.txt
メモ

Discard a change that was added

To throw away a change that was git added, including the file content, pass both --staged and --worktree. The source is HEAD, so the file returns to the state of the last commit.

console
$ git add price.txt

$ git restore --staged --worktree price.txt

$ git status
On branch main
nothing to commit, working tree clean

$ cat price.txt
送料: 400円
手数料: 200円

Edits made after git add go back to the content at the time of the add

Here, I changed the handling fee to 100 yen and ran git add, then changed it again to 50 yen. Comparing git diff (working tree against stage) and git diff --staged (stage against the last commit) shows that there are two stages of changes.

console
$ git diff
diff --git a/price.txt b/price.txt
index fbe82df..a94015d 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
 送料: 400円
-手数料: 100円
+手数料: 50円

$ git diff --staged
diff --git a/price.txt b/price.txt
index ea0fb19..fbe82df 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
 送料: 400円
-手数料: 200円
+手数料: 100円

When I ran a plain git restore here, the file went back to 100 yen, the content at the time of git add, not to 200 yen from the last commit.

console
$ git restore price.txt

$ cat price.txt
送料: 400円
手数料: 100円

$ git status --short
M  price.txt

In git status --short, the left column shows the state of the stage and the right column shows the state of the working tree (official git status reference). Because M is in the left column, the 100 yen change is still in the stage. To go all the way back to the last commit, use --staged --worktree.

console
$ git restore --staged --worktree price.txt

$ cat price.txt
送料: 400円
手数料: 200円

$ git status --short

Take the content from an earlier commit

Passing a commit to --source restores the file to the content it had in that commit. HEAD~1 refers to the commit just before the latest one (its first parent) (official reference on revision specification).

console
$ git restore --source=HEAD~1 price.txt

$ cat price.txt
送料: 500円
手数料: 200円

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   price.txt

no changes added to commit (use "git add" and/or "git commit -a")

The shipping fee changed to 500 yen, the value from the commit before the latest one. The branch and commits did not move; only the file in the working tree was overwritten. The official git reference, in its "Reset, restore and revert" section, also explains that git restore does not update branches. After running git restore price.txt, the file returned to 400 yen.

Undo a deletion that was committed

This is the case where you committed a deletion of a file and later found that you needed it. Neither the stage nor the latest commit contains news.txt, so a plain git restore gives an error.

console
$ git log --oneline
19dacae お知らせを削除
64db4ca 送料を400円に変更
54cce3b 最初のコミット

$ git restore news.txt
error: pathspec 'news.txt' did not match any file(s) known to git

Passing the file name to git log shows which commit deleted it. For --source, pass the commit just before the one that deleted it. Here the deleting commit (19dacae) is the latest, so the one before it is HEAD~1.

console
$ git log --oneline -1 -- news.txt
19dacae お知らせを削除

$ git restore --source=HEAD~1 news.txt

$ git status --short
?? news.txt

$ cat news.txt
営業時間: 10時から18時

The file came back, but git status --short shows ??, meaning Git does not track it. --source alone only places the file in the working tree and does not put it in the stage. To include it in a commit again, run git add and then commit.

console
$ git add news.txt && git commit -q -m "お知らせを戻す"

$ git status --short

Restore all changes at once, or only part of them

If you pass . instead of a file name from the top folder of the repository, all changes are restored at once. Both the edited file and the deleted file were restored in one step.

console
$ git status --short
 D README.md
 M news.txt
 M price.txt

$ git restore .

$ git status --short

Conversely, if you want to discard only part of the changes, add -p. For each hunk of changes, you are asked whether to discard it (y) or keep it (n). Normally you answer at the keyboard, but here I piped the answers with printf so the output could be recorded: y for the change to news.txt and n for the change to price.txt, in that order. (For this reason, the answer characters do not appear on screen.)

console
$ printf "y\nn\n" | git restore -p
diff --git a/news.txt b/news.txt
index a91b5d8..d6da573 100644
--- a/news.txt
+++ b/news.txt
@@ -1 +1,2 @@
 営業時間: 10時から18時
+臨時休業のお知らせ
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? 
diff --git a/price.txt b/price.txt
index ea0fb19..eb91650 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
-送料: 400円
+送料: 0円
 手数料: 200円
(1/1) Discard this hunk from worktree [y,n,q,a,d,e,?]? 

$ git status --short
 M price.txt

$ cat price.txt
送料: 0円
手数料: 200円

Only the change to news.txt was discarded, and the change to price.txt was kept.

Files that were never added cannot be restored with git restore

A file you just created and never ran git add on has no content stored in Git. Passing it to git restore gives an error.

console
$ git status --short
?? memo.txt

$ git restore memo.txt
error: pathspec 'memo.txt' did not match any file(s) known to git

For the same reason, if you deleted or edited a file that was never added or committed, git restore cannot bring it back.

Relationship to git checkout —

Older articles sometimes use git checkout -- <file name> to discard changes. According to the Git 2.23.0 release notes, git switch and git restore were added to separate the two jobs git checkout used to handle: switching branches and taking files out. In the test environment, git checkout -- price.txt discarded the edit in the same way as git restore price.txt.

console
$ git checkout -- price.txt

$ cat price.txt
送料: 400円
手数料: 200円

$ git status --short

​Posted by 86