Features Tips & Tricks Tutorials

Feature Spotlight: Tracking code changes with Local History

Happy Friday!

Today I’d like to highlight the one of the top features that saves you from unexpected changes in your code and keeps things on track – Local History. This feature exists in PyCharm for a long while, but it’s worth to talk about it once again!

Note: Local History is available in both PyCharm Professional Edition and PyCharm Community Edition.

Local history is the way for PyCharm to track all your local personal changes to your project files. It simply expands the VCS functionality for you locally. When developing, there are constant small changes in the code. I bet you’ve had occasions when you needed the information about code changes, but the version control systems couldn’t help you because they track and compare differences only between committed versions – you just can’t commit so often to have a track of all your small changes. Some local changes between commits are unnoticed for VCS and this is where Local History comes to the rescue!

So let me show you how it works:

When editing, you can open local history by simply right-clicking on any file, and go to Local History | Show history:


This will show you the changes of your file, including commits, external changes, some major refactorings that happened, and just other basic changes. And then once you’re on one of these changes it shows you the diff of the file:


You can scroll down and revert any of the changes that you want by clicking the icons to insert the old code or remove code, or by simply reverting the entire file by right-clicking and selecting Revert or Create Patch:


You can also select a block of text and only show the changes for that selection. So select a code snippet and click Show history for selection:


You can also select an entire directory or your entire project – do “show history” on a directory or project and it’ll show you the list of files that have changed in your local history. And then once you find a file that you want you can double click on it to bring the diff for that file in that copy of the history:


You can also invoke local history from the keyboard by using Alt + Backquote and going down to Local history | Show history. That’s just a different way to invoke it.

Last but not least, at any time you can do Local history | Put label, give the label a name and now have a custom label which is stored in local history and lets you view your file’s state at the time you added the label:


As you can see Local History is just the PyCharm’s way of tracking your personal changes. That isn’t something that is shared across your project. It just tracks local changes to files, keeping everything in its history: external changes, commits, regular edits, etc.

I know quite a few times when it actually saved developers from a system crash consequences or helped when VCS just wasn’t enough. Sometimes you could really use that extra hand from your IDE.

So in case you’ve lost some files or made some unexpected changes in your code – everything is still in the PyCharm memory. Check it out!

See you next week!

image description