Git: How To Discard Unstaged Changes In One Command

- 1 answer

I have a Jupyter Notebook which I often use to try out new functionality in my code. The Jupyter Notebook is also tracked by Git. After adding the important changes (to different file(s), not the changes to the Notebook) to the index and committing them, I find this Notebook file ruining my clean git status. In order to restore its previous state, I find myself doing

git stash push convenient_notebook.ipynb
git stash drop

frequently -- always in fear I did something wrong and git stash drop a change I still need.

Is there a single command which trashes all unstaged changes, ideally with the option to specify a file?
This answer to a similar question suggests git checkout -- ., but I don't understand why this works and whether it is appropriate.
This answer to the same question suggests roughly what I do right now, i.e. the two successive commands I'd like to get rid of.



One of the answers you linked already mentions how to do it in a one liner:

git restore convenient_notebook.ipynb

If you’re finding yourself doing that on most commits, it may be easier to add the file to your .gitignore

And then only add changes when you really want to:

git add —force convenient_notebook.ipynb