How to Revert Local Changes with Git Restore

Daniel Pericich
3 min readJan 27, 2022
Photo by Jeff Kingma on Unsplash

Dependencies are a great way to avoid reinventing the wheel for every bit of functionality that you may want to use in your apps. However, they don’t come without a cost. Each dependency is prone to issues with version update breaking changes, new hardware losing bindings (looking at you M1 chip in 2020) and even losing licenses to distribute the dependency.

Of course there are workarounds, but if you are working on a large legacy project this can cause inconveniences of manually adding specific gem dependency versions. A few months ago I found myself in a cycle of updating my Gemfile and Gemfile.lock and dropping these changes to then add them again every time I switched between branches.

While it was only 2 files, I got tired pretty quickly of clicking through VS Code to find, select and delete these files every time I switched between branches. This led me to ask the question, “How could I speed up the process from the command line?” From this question I found the command ‘git restore’.

What is git restore?

Though there’s a learning curve to getting familiar with the command line, the benefits of shaving seconds plus off of every action you perform adds up over the days and weeks. GUI’s are great for beginners, but being able to work out of the terminal will speed up a lot of your workflow.

The issue I described before led me to look for a way to drop local changes in my repo from the command line. There are many articles to remove changes that have been locally committed, but not many about dropping uncommitted changes. Searching more led me to find git restore.

The git restore command lets you accomplish a number of different functions. Git restore ‘restores specified paths in the working tree with some contents from a restore source.’ What this means is that restore lets you specify a set of files, or a single file to revert back to their base state before any changes were made. This was perfect to drop the Gemfile changes from specifying gem versions.

My most used function is ‘git restore .’ which drops all uncommitted changes in your local repo. For my workflow I would commit all the changes I wanted for my branches, then run ‘git restore .’ to drop my lingering Gemfile and Gemfile.lock file changes.

If you want to drop specific files, you can use ‘git restore <file-path>’ to drop a desired file. These two commands will cover most of your needs, but feel free to check out the attached docs to find more specific use cases.

Conclusion

If you find this command helpful, I’d highly recommend diving into the git documentation. One of my early managers told me that 90% of git’s usage can be completed with 10% of the total commands. He said knowing how to do the other 90% would set you aside from other developers. Let me know in the comments what other 90% git commands you know and love!

Notes

https://git-scm.com/docs/git-restore

--

--

Daniel Pericich

Former Big Beer Engineer turned Full Stack Software Engineer