Video in progress

Junior 🌿 Git

How do you discard uncommitted changes in a file and go back to the state of the last commit?

git restore <file> returns the change on disk to the state of the last commit (more precisely, of staging). If the file was already git added, first run git restore --staged, then a plain restore.

📖 Answer

# changed on disk only
git restore src/main/resources/application.yml

# I had already run add
git restore --staged src/main/resources/application.yml
git restore src/main/resources/application.yml

# throw away everything (be careful)
git restore .

These actions cannot be undone: an uncommitted change is not in Git's history, so even reflog will not help. If in doubt, run git stash first, and if you do not need it later, git stash drop.

restore does not delete newly added files that are not tracked yet. For those, use git clean -n (a dry run first), then git clean -f.