PyCharm does not have a built-in, automatic feature to shelve changes automatically when switching branches. However, you can achieve a similar workflow with some manual steps or by using Git hooks or external scripts combined with PyCharm's shelve functionality.
What PyCharm offers natively:
- Manual Shelving: You can manually shelve your changes before switching branches via
VCS → Shelve Changes.
- Unshelve: After switching branches, you can unshelve the changes.
- Local History: PyCharm keeps local history, but it’s not tied to branch switching.
Why automatic shelving on branch switch is tricky:
- Branch switching is a Git operation, and PyCharm doesn’t automatically trigger shelving on branch changes.
- Shelving is a PyCharm-specific feature, not a Git feature, so Git hooks can’t directly shelve changes.
Workarounds / Possible Solutions
1. Use Git hooks + external script (advanced)
You can create a Git pre-checkout or pre-switch hook that runs a script to stash changes automatically before switching branches. This is outside PyCharm but achieves a similar effect.
- Git stash is the closest native Git feature to shelving.
- You can then manually unshelve (or unstash) in PyCharm or via Git.
Example:
Create .git/hooks/pre-checkout (make executable):
#!/bin/sh
git stash push -m "Auto stash before branch switch"
This stashes changes before switching branches.
Note: This is not shelving but stashing, which is similar.
2. Use PyCharm macros or external tools (semi-automatic)
- You can create a macro in PyCharm that shelves changes and then switches branches.
- Bind this macro to a shortcut.
- This requires manual triggering but automates the process.
3. Feature requests / Plugins
- Check JetBrains Plugin Repository for plugins that might automate shelving on branch switch.
- Submit a feature request to JetBrains for automatic shelving on branch switch.
Summary
| Method | Automatic? | Requires manual step? | Notes |
|---|
| PyCharm shelving | No | Yes | Manual shelving/unshelving |
| Git stash + hooks | Semi | No | Uses Git stash, not shelving |
| PyCharm macro + shortcut | Semi | Yes | Automates shelving + branch switch |
| Plugin / Feature request | TBD | TBD | Check JetBrains marketplace |
If you want, I can help you write a script or macro for your preferred approach! Would you like that?