How to discard changes in Git

If you just want to clean unstaged changes on Git in your current working directory execute the following ‘Git discard all changes’ command ’:

git checkout — .

How to Delete a Git Branch Locally and Remotely

If however you are referring to a specific existing file, simply add the file path as follows :

git checkout — thepathtothefile/to/discard

For newer Git releases however you would want to use the restore command :

git restore .                                         [Git discard changes command]

And for a specific file :

git restore thepathtothefile/to/discard               [Git revert changes for a file]

Answer due to Tobi.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply