How to discard local modifications in git?

Written on by Mandeep Singh in Mistakes Correction

Everybody has it's own way of writing code and there are times when you try out a few ways of creating a functionality or fixing that nasty bug which turns out to be not so good way of doing it. You want to discard your current changes, you can do it quite easily by checking out the modified files.

git checkout modified-file.ext

That's all you need..



But wait! what if you want to discard more then one file?

git checkout modified-file.ext another-modified-file.ext one-more-modified-file.ext

You can add as many files as you want and not just files it works with complete directories too.

git checkout ./modified-directory

That's everything in modified-directory.



But wait! what if you have too many files to discard and it's taing forever doing it one by one or you simply want to discard everything?

git checkout .

Yup a dot.. that's all you need.

Note: this method helps you discard any modified files from your working directory, if you have created any new ones, they needed to be removed the good old fashioned way or rm.

Sign up below to receive updates, tips and more handy tutorials. It’s good stuff & I don’t spam.