• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Computing / git restore

git restore

Last updated on October 6, 2020 by Sal Ferrarello

Git version 2.23.0 introduced a new command git restore. This command does a subset of what git checkout does (because git checkout does so many different things). You can continue using git checkout for this functionality but the idea is git restore is clearer for those starting out.

Error Message

If you get the message,

git: ‘restore’ is not a git command. See ‘git –help’.

you’re probably running an older version of git.

In this case, you can run git checkout instead of git restore.

Discard Changes

When you have changes but want to discard them, git restore will discard the changes and restore the file(s) to the state they were in the last time you made a commit.

This is done with git restore <filename> or git restore <directory> (when a directory is given all of the changes in the directory will be discarded).

Old Way

git checkout myfile.txt

New Way

git restore myfile.txt

Other Examples

Discard all changes in the entire repo.

git restore :/

Discard all changes in the current directory (and sub-directories)

git restore .

See Also

Git version 2.23.0 also introduced git switch.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Computing, Draft Tagged With: Git

Reader Interactions

Comments

  1. Chaitanya says

    March 1, 2021 at 7:54 am

    Thanks

    Reply
  2. Someone says

    April 7, 2021 at 9:18 pm

    Thanks, really useful!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in