PyCharm

How to Undo in PyCharm: Shortcut Guide

How to Undo in PyCharm: Shortcut Guide

Most mistakes in PyCharm are one Ctrl+Z away (Cmd+Z on macOS), as long as the IDE has stayed open since you made them. Redo sits on Ctrl+Shift+Z (Cmd+Shift+Z), and both act on the editor history of the file you’re in.

Close PyCharm and that history is gone. Older or deleted work comes back through Local History, which keeps revisions for the last 5 working days by default (PyCharm 2026.2 Help).

Git covers whatever is left. Rollback handles uncommitted changes, and once something is committed you have Undo Commit, Revert Commit and Reset Current Branch to Here.

What are the undo and redo shortcuts in PyCharm?

YouTube player

On Windows and Linux you press Ctrl+Z to undo and Ctrl+Shift+Z to redo. macOS swaps Ctrl for Cmd.

Both commands also live in the Edit menu, and the PyCharm 2023.1 Help lists them as icons on the main toolbar. Find Action works too. Hit Ctrl+Shift+A (Shift+Cmd+A on macOS), type “undo” and run it.

Redo only works right after an undo, and any new edit ends the chain.

Windows users coming from Word often reach for Ctrl+Y. In PyCharm’s default keymap that key deletes the current line (PyCharm 2026.2 Help), so it never redoes anything unless you rebind it.

Line moves, commenting and a handful of other editing keys are covered in the list of PyCharm keyboard shortcuts.

What a single undo step reverts in PyCharm

One undo press reverses one logical step in the active file, not one keystroke. PyCharm groups your typing and closes a step when you press Enter, move the mouse cursor, use a navigation shortcut, cut or paste, or press Tab (PyCharm 2018.2 Help).

Undo and redo repeat as many times as you need within a session. The undo history is lost when PyCharm exits.

Why is Python conquering the tech world?

Explore comprehensive Python statistics: market dominance, developer preferences, salary insights, and growth trends across industries.

Discover Python Insights →

The step rules above come from the 2018.2 edition. The 2026.2 Help page on source code mentions undo and redo in one line only.

Undoing a refactoring or Reformat Code

Refactorings and reformatting count as complex operations, so PyCharm asks for confirmation when you undo or redo one (PyCharm 2018.2 Help).

Changes made through code refactoring tools such as Rename reverse with Ctrl+Z, and the PyCharm 2026.2 Help gives exactly that instruction.

Reformat Code runs on Ctrl+Alt+L in the default Windows and Linux keymap (PyCharm 2026.2 Help) and undoes the same way.

If undo no longer reaches the refactoring, after a restart for example, Local History can restore the earlier state.

New to Python or just need a quick reference? List comprehensions, built-in functions, and core syntax - including lambda, dict methods, and file I/O - is on one page in the Python Cheat Sheet.

Undoing file creation and deletion

Creating and deleting files sit in the same complex-operation group, and Ctrl+Z asks before it reverses one (PyCharm 2018.2 Help). That works while the session lives.

After a restart the undo history is gone, and a deleted file needs Local History instead.

Why undo stops working in PyCharm

Undo fails when its history is gone, or when the change sits too far back to reverse cleanly. The documented cases are in the table.

SituationCauseFallback
PyCharm was restartedUndo history is lost on exitLocal History
Change is far back, among edits to keepUndo would discard the later edits tooLocal History, restore one fragment
Deleted file never committedNo commit holds a copyLocal History, Revert Selection
Redo vanished after Ctrl+Y on WindowsCtrl+Y runs Delete Line, a new editRebind Ctrl+Y, or use Ctrl+Shift+Z

The restart case is the one that catches people off guard. Reopen the project and the Edit menu offers nothing to undo.

The distant-change case is spelled out in the PyCharm 2026.2 Help. Undo can’t reach a change that far back without forcing you to discard other changes along the way.

The Ctrl+Y case is a long-running complaint in the JetBrains Support community. Press it after several undos and it deletes a line, and that new edit replaces the redo history.

Three of those four rows end at Local History, which is the first place to look because it needs no commit.

How to restore earlier code with Local History

Right-click in the editor, choose Local History, then Show History, pick a revision, and revert the file or copy back one fragment. Local History records the project state while you work, including around test runs and deploys, so it covers the stretch between your last commit and now.

It works without version control in the project, and it records changes made outside the IDE as well (PyCharm 2026.2 Help).

Local History does not replace a repository. The same Help page says it is cleared when you install a new version of PyCharm, and that it has a retention period and a maximum size.

Before a risky change, add a label yourself. Go to File, Local History, Put Label and type a name. PyCharm also labels some revisions on its own, for example after a test run or deployment, and at commit time.

Restoring a deleted file

This route applies to files that were not under version control.

  1. In the Project tool window (Alt+1), right-click the node that held the deleted file and choose Local History, then Show History.
  2. In the left pane, select a revision that contains the file, right-click the file, and choose Revert Selection.

The same dialog opens for a whole folder or project when several files changed.

Restoring a code fragment

Use this when you deleted a block ten edits ago and want to keep everything typed since.

  1. Right-click anywhere in the editor and choose Local History, then Show History. The left pane lists saved revisions with timestamps, and the right pane shows a diff against the current file.
  2. Select the revision that contains the lost code.
  3. In the diff, find the fragment and click the chevron button to copy it from the left pane.

Type a snippet of the lost code into the search bar of the dialog to find the right revision faster. To roll the whole file back instead, right-click the revision and choose Revert Selected and Later Changes.

The chevron copies one change and leaves the rest of the file alone.

Changing the retention period

By default, Local History keeps revisions for the last 5 working days, meaning days when the file was modified (PyCharm 2026.2 Help). Where you change that depends on the edition.

  • In the 2026.2 Help it sits under Settings (Ctrl+Alt+S), Advanced Settings, IDE section, in the field Duration of storing changes in Local History.
  • The 2021.1 Help puts it in the Registry, opened through Find Action, under the key localHistory.daysToKeep.
  • A JVM option does the same job, and -DlocalHistory.daysToKeep=30 sets 30 days.
  • Setting the period to 0 disables it.

The Help also names a maximum size on top of the period, so a longer window does not promise longer history.

How to roll back changes and commits in Git from PyCharm

Uncommitted edits go through Rollback in the Commit tool window. Anything already committed is handled from the Git tool window. Undo Commit takes back the last commit, Revert Commit suits one that was pushed, and Reset Current Branch to Here pulls a branch back across several.

Rolling back uncommitted changes

Rollback discards every change made to the selected files since the last commit (PyCharm 2026.2 Help).

  1. Open the Commit tool window (Alt+0) and select the files to revert.
  2. Choose Rollback from the context menu, or press Ctrl+Alt+Z.
  3. Check the file list in the dialog. Tick Delete local copies of added files if newly added files should go too.

The reverted files also drop out of the active changelist.

Shelve Changes, in the same context menu, keeps a copy instead. It stores the selection as a patch and resets the local files, and a shelved change can be applied as many times as needed.

Ctrl+Shift+H shelves without a dialog and Ctrl+Shift+U unshelves. Unversioned files can’t be shelved at all.

Git stash does a similar job. Stashes come from Git and cover all uncommitted changes, while a shelf takes only the files you pick.

Undoing the last commit

Undo Commit takes the last commit in the current branch apart and moves its changes back into a changelist.

Open the Git tool window (Alt+9), switch to the Log tab, select the last commit, and choose Undo Commit. Pick the target changelist in the dialog. The commit message becomes the new changelist name by default.

A commit pushed to a protected branch can’t be undone this way, though. A protected branch is one where force push is not allowed, and PyCharm marks a branch as protected on its own when it is protected on GitHub.

Reverting a pushed commit versus resetting a branch

A pushed commit with a mistake in it calls for Revert Commit. It follows the logic of a Git revert and writes a new commit that reverses the old one, so the history stays intact.

Select several commits in the Log and PyCharm creates one revert commit for each. Deselect files in the dialog to revert only part of a commit.

Reset rewrites where the branch points. Reset Current Branch to Here runs a Git reset that moves HEAD to the selected commit, and the Git Reset dialog decides what happens to the index and working tree.

  • With Soft, changes from later commits come back staged, in the Commit window.
  • Mixed leaves them in the working tree, unstaged.
  • Hard discards everything after the target commit, staged and committed alike.
  • Keep drops the later commits but leaves your local changes intact.

Prefer Keep over Hard whenever local edits matter.

Drop Commit removes a pushed commit without a trace, but it needs a force push and fails on protected branches. On a shared branch, revert is the safer default.

Which method to use: undo, Local History, Rollback or Undo Commit

Pick the narrowest tool that reaches the mistake. Undo covers the open file, Local History covers older or deleted work, Rollback covers uncommitted work across files, and the Git tools cover commits.

MethodReachNeeds GitEntry point
UndoActive file, current sessionNoCtrl+Z
Local HistoryFile, folder or project, within retentionNoLocal History, Show History
RollbackUncommitted changes in selected filesYesCommit tool window, Ctrl+Alt+Z
Undo CommitLast commit in the current branchYesGit tool window, Log tab
ResetBranch HEAD back to a chosen commitYesLog tab, Reset Current Branch to Here
  • A mistake in the open file, made this session, is a job for undo.
  • Local History handles a change from before the session, and any deleted file.
  • For unwanted edits spread across several uncommitted files, use Rollback.
  • If the last commit is the problem and the branch allows force push, Undo Commit is enough.
  • On a shared branch, use Revert Commit instead.
  • When several commits need to go, Reset with Keep or Soft does it.

Undo and Rollback differ in scope more than in strength. Undo steps back through recent edits one at a time, and Rollback resets the selected files to the last commit in one action.

The Help also names Local History as a recovery source when the computer restarts unexpectedly before you act. That is the situation the undo stack cannot cover.

Changing the undo and redo shortcuts in the keymap

Rebind an action on the Keymap page of the Settings dialog.

  1. Press Ctrl+Alt+S, select Keymap, and type Undo or Redo in the search field.
  2. Right-click the action and choose Add Keyboard Shortcut.
  3. Press the key combination in the Keyboard Shortcut dialog. A warning appears if it clashes with an existing binding.
  4. Click OK. Right-click the action and choose Reset Shortcuts to return to the default.

To make Ctrl+Y redo on Windows, add it to Redo in step 3 and read the clash warning before saving. JetBrains Support community answers recommend exactly this reconfiguration.

If a shortcut does nothing, click Find Action by Shortcut on the Keymap page and press the keys. PyCharm shows which action holds them.

Predefined keymaps cannot be edited. The first change duplicates the keymap into a custom copy that stores only your differences from its parent (PyCharm 2026.2 Help).

That copy sits in the keymaps folder of the IDE configuration directory. Copy the file to another installation and select it there to share your bindings.

  • On macOS the default is the macOS keymap. IntelliJ IDEA Classic resembles the Windows default, and macOS System Shortcuts exists to avoid clashes with the system (PyCharm 2026.2 Help).
  • The Eclipse, Visual Studio, NetBeans and Sublime Text keymaps leave undo on Ctrl+Z (Cmd+Z in the macOS variants), per the IntelliJ IDEA 2026.2 Help.
  • Emacs is messier. The IntelliJ IDEA 2026.2 Help lists Ctrl+Shift+Minus, while JetBrains Support posts from the PyCharm 3.4 and 5.0.3 era name Ctrl+/ and Ctrl+\_. Check the Keymap page in your build.
  • Older IdeaVim README versions say the plugin uses the IntelliJ Platform’s own undo and redo, so u and Ctrl+R can behave differently from Vim, and the current README no longer repeats that. Shortcut clashes are resolved under Editor, Vim and in Keymap, or with mappings in the ~/.ideavimrc file (IdeaVim README).

Undo in PyCharm FAQ

How do I undo git add in PyCharm?

By default, PyCharm stages modified files through changelists automatically, so there is no add to undo. Leave the file unselected in the Commit tool window, or move it to another changelist.

Prefer the staging concept? Turn on Enable staging area in the Version Control, Git settings.

Can undo bring back changes after the file was saved?

Yes, while the PyCharm session is still running. The Help ties the loss of undo history to exiting PyCharm, not to saving.

After a restart, Local History is the route back.

Why is undo greyed out in Remote Development mode?

Users reported in 2022 that Undo and Redo were greyed out, and the shortcuts did nothing, in sessions through JetBrains Gateway. The host logs showed errors from the undo manager.

JetBrains collected logs under the issue CWM-5999, so check that ticket for the current status.

Where does PyCharm store Local History, and can it be shared?

Local History lives as binary files in the LocalHistory subdirectory of the PyCharm system directory, for example ~/.cache/JetBrains/<product><version> on Linux. It does not support shared access.

To pass a revision to a colleague, use Create Patch in the Local History dialog.

Why does Ctrl+/ beep when I undo on macOS with the Emacs keymap?

A JetBrains Support post on PyCharm 3.4 and 5.0.3 reported that undo worked but the system beeped on Ctrl+/, while Ctrl+\_ and the Edit menu stayed silent. A reply linked the issue IDEA-163077.

The IntelliJ IDEA 2026.2 Help lists Ctrl+Shift+Minus for Emacs undo, so check the Keymap page in your build.

Setting Up Recovery Before You Need It

Undo in PyCharm covers one open session, so recovery needs two layers behind it, set up before the mistake. Each layer expires at a different point, which fixes the order.

Commit before a risky change, and stretch the Local History window if the task will run long. A label on the state you can return to helps too.

Git goes first, since it is the only layer that survives every expiry event. Closing PyCharm ends the undo history, an upgrade clears Local History, and the retention window eventually eats the rest.

Pushing adds an off-disk copy. On a protected branch it also turns the fix from Undo Commit into Revert Commit, so push work in progress to a feature branch.

Connecting a PyCharm project to GitHub puts that copy in place.

Verified against the PyCharm 2026.2 Help on October 7, 2026. A release that relocates the Local History setting again would change the retention steps above.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.