How to Discard Changes in Git (with git restore)
No matter how experienced you are as a programmer, not all of your code will always work at the first try. Luckily, Git allows you to discard and undo any of your changes, providing a safety net for your work.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Discarding Local Changes in a File
Changes that haven't been committed are called "local" changes — they exist in your working copy but haven't been wrapped in a commit yet.
To discard all uncommitted local changes in a specific file:
$ git restore index.html
Be careful: you cannot get these changes back once you've discarded them.
To discard all local changes across the entire project at once:
$ git restore .
If you also have untracked (new) files you want to remove:
$ git clean -fd
Tip
Discarding Changes in Tower
In case you are using the Tower Git client, you can discard local changes in a file simply from its contextual menu — or even discard only _parts_ of your changes while keeping the rest:
Although it's not possible in Git, Tower allows you to undo any wrongfully discarded changes with a simple shortcut: CMD+Z (or CTRL+Z on Windows)!
Unstaging Changes
If you've run git add but haven't committed yet and want to remove files from the staging area (without touching the actual file contents), use the --staged flag:
$ git restore --staged index.html
To unstage everything at once:
$ git restore --staged .
For fine-grained control, the --patch flag lets you step through each changed chunk interactively and choose whether to unstage or discard it:
$ git restore --patch index.html
Tip
Discarding / Unstaging Chunks or Lines in Tower
Using the Tower Git client, you can easily select the exact chunks & lines you want to stage, unstage, or discard:
Restoring a Specific Revision of a File
You can restore a file to how it looked at any point in history using the --source flag:
$ git restore --source 7173808e index.html
$ git restore --source master~2 index.html
The first example restores the file as it was in commit 7173808e. The second restores it as it was two commits before the current tip of master.
Saving Changes to the Stash
If you're not sure whether you'll need your changes later, save them temporarily on the stash instead of discarding them:
$ git stash --include-untracked
This leaves you with a clean working copy while preserving the changes. To restore them later:
$ git stash pop
Tip
Stashing in Tower
In case you are using the Tower Git client, saving to and restoring from the Stash can be performed right from the toolbar:
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.