PyCharm 2017.3 Help

Work on several features simultaneously

Sometimes you need to switch between different tasks with things left unfinished and then return back to them. PyCharm provides you with a few ways to conveniently work on several different features without losing your work:

Shelve changes

Shelving is temporarily storing pending changes you have not committed yet. This is useful, for example, if you need to switch to another high priority task and you want to set your changes aside to work on them later.

With PyCharm, you can shelve both separate files and entire changelists.

Once shelved, a change can be applied as many times as you need by unshelving and subsequently restoring it on the shelf.

Put changes to a shelf

  1. Open the Version Control tool window (Alt+9) and switch to the Local Changes tab.
  2. Select the files or a changelist you want to put to a shelf. On the main Version Control menu or on the context menu of the selection, choose Shelve changes.
  3. In the Shelve Changes dialog, review the list of modified files.
  4. In the Commit Message field, enter the name of the shelf to be created and click the Shelve Changes button.

You can also shelve changes silently, without displaying the Shelve Changes dialog. To do this, select a file or a changelist you want to shelve, and click the Shelve Silently icon shelve silently icon on the toolbar, or press Ctrl+Alt+H. The name of the changelist containing the changes you want to shelve will be used as the shelf name.

Unshelve changes

Unshelving is moving postponed changes from a shelf to a pending changelist. Unshelved changes can be filtered out from view or removed from the shelf.

  1. In the Shelf tab of the Version Control tool window, select a changelist or files you want to unshelve.
  2. Press Ctrl+Shift+U or choose Unshelve from the context menu of the selection.
  3. In the Unshelve Changes dialog that opens, specify the changelist you want to restore the unshelved changes to in the Name field. You can select an existing changelist from the drop-down list or type a name for a new changelist to be created containing the unshelved changes. You can enter the description of the new changelist in the Comment field (optional).

    If you want to make the new changelist active, select the Set active option. Otherwise, the current active changelist remains active.

  4. If you want PyCharm to preserve the context of a task associated with the new changelist on its deactivation and restore the context then the changelist becomes active, select the Track context option (see Managing Tasks and Contexts for details).
  5. If you want to remove the changes you are about to unshelve, select the Remove successfully applied files from the shelf option. The unshelved files will be removed from this shelf and added to another changelist and marked as applied. They will not be removed completely until deleted explicitly by clicking the clean unshelved changes icon on the toolbar, or selecting Clean Already Unshelved from the context menu.
  6. Click OK. If conflicts occur between the patched version and the current version, resolve them as described in Resolving Conflicts.

You can also unshelve changes silently, without displaying the Unshelve Changes dialog. To do this, select a file or a changelist you want to unshelve, and click the Unshelve Silently icon unshelve silently icon on the toolbar, or press Ctrl+Alt+U. The unshelved files will be moved to the active pending changelist.

Restore unshelved changes

PyCharm lets you reapply unshelved changes if necessary. All unshelved changes can be reused until they are removed explicitly by clicking the clean unshelved changes icon on the toolbar, or selecting Clean Already Unshelved from the context menu.

To restore applied changes on the shelf do the following:

  1. Make sure that the Show Already Unshelved show already unshelved toolbar option is enabled.
  2. Select the files or the shelf you want to restore.
  3. On the context menu of the selection, choose Restore.

Apply external patches

You can import patches created inside or outside PyCharm and apply them as shelved changes.

  1. In the Shelf tab of the Version Control tool window, choose Import Patches from the context menu.
  2. In the dialog that opens, select the patch file to apply. The selected patch appears in the Shelf tab as a shelf.
  3. Select the newly added shelf with the patch and choose Unshelve Changes from the context menu of the selection.

Automatically shelve base revision

It may be useful to configure PyCharm to always shelve base revisions of files that are under Git version control. To do this, open the Settings dialog (Ctrl+Alt+S), select the Version Control | Shelf node on the left and select the Shelve base revisions of files under distributed version control systems option.

If this option is enabled, the base revision of files will be saved to a shelf that will be used during a 3-way merge if applying a shelf leads to conflicts. If it is disabled, PyCharm will look for the base revision in the project history, which may take a while; moreover, the revision that the conflicting shelf was based on may be missing (for example, if the history was changed as a result of the rebase operation).

Change the default shelf location

By default, the shelf directory is located under your project directory. However, you may want to change the default shelf location. This can be useful, for example, if you want to avoid deleting shelves accidentally when cleaning up your working copy, or if you want to store them in a separate repository allowing shelves to be shared among your team members.

  1. Open the Settings dialog (Ctrl+Alt+S) and select the Version Control | Shelf node on the left.
  2. Click the Change Shelves Location button and specify the new location in the dialog that opens.
  3. If necessary, select the Move shelves to the new location option to move existing shelves to the new directory.

Watch this video tutorial on how to benefit from shelves to be able to switch to a different task without losing unfinished work:

Stash changes

Sometimes it may be necessary to revert your working copy to match the HEAD commit but you do not want to lose the work you have already done. This may happen if you learn that there are upstream changes that are possibly relevant to what you are doing, or if you need to make some urgent fixes.

Stashing involves recording the difference between the HEAD commit and the current state of the working directory (stash). Changes to the index can be stashed as well.

Unstashing involves applying a stored stash to a branch.

You can apply a stash to an existing branch or create a new branch on its basis.

A stash can be applied as many times as you need to any branch you need, just switch to the required branch. Keep in mind that:

  • Applying a stash after a series of commits results in conflicts that need to be resolved.
  • You cannot apply a stash to a "dirty" working copy, that is a working copy with uncommitted changes.

Save changes to a stash

  1. From the main menu, choose VCS | Git | Stash Changes.
  2. In the Stash dialog that opens, select the appropriate Git root and make sure that the correct branch is checked out.
  3. In the Message field describe the changes you are about to stash.
  4. To stash local changes and bring the changes staged in the index to your working tree for examination and testing, select the Keep index option.
  5. Click Create Stash.

Apply a stash

To apply a stash, do the following:

  1. From the main menu, choose VCS | Git | Unstash Changes.
  2. Select the Git root where you want to apply a stash, and make sure that the correct branch is checked out.
  3. Select the stash you want to apply from the list.

    If you want to check which files are affected in the selected stash, click View.

  4. To remove the selected stash after it is applied, select the Pop stash option.
  5. To apply stashed index modifications as well, select the Reinstate Index option.
  6. If you want to create a new branch on the basis of the selected stash instead of applying it to the branch that is currently checked out, type the name of that branch in the As new branch field.

To remove a stash, select it in the list and click Drop. To remove all stashes, click Clear.

Group changes into different changelists

When you are working on several related features, you may find it convenient to group changes into different changelists. This approach has its pros and cons as opposed to using feature branches to work on multiple tasks.

Pros:

  • You can easily switch between different logical sets of changes and commit them separately from each other.
  • Unlike using branches for the same purpose, you have all your changes at hand without having to switch between branches which can take a while if your project is really large.
  • It's convenient to test how different features work together.
  • You can remote-run a changelist on a build server.

Cons:

  • While using changelists may seem a more lightweight option compared to branches, it's not safe as there's no backup for your changes until you have committed and pushed them. If something happens to your local working copy, all your changes will be lost as they are not part of Git project history.
  • Using changelists only works if changes do not overlap. If different tasks affect the same file, you cannot separate these changes and put them into separate changelists.
  • No atomic testing of features is possible.
  • No collaboration on the same feature is possible. Also, you cannot make contributions from different machines unless you send patches with changes through email, which may not be very convenient.

All changelists are displayed in the Local Changes tab of the Version Control tool window. All modified files are automatically placed in the active changelist, which is the Default changelist unless you have created a different one and made it active.

To create a new changelist, click add on the toolbar.

To make a non-default changelist active, right-click it and choose Set Active Changelist from the context menu.

To move changes between changelists, select the file you want to move and click moveChangelistItems on the toolbar, or choose Move to Another Changelist from the context menu.

  1. Select the Set active option if you want to make the changelist with the changes you are about to discard the active changelist.
  2. Select the Track context option if you want PyCharm to remember your context and reload currently opened files in the editor when this changelist becomes active.

Use feature branches

A branch in Git represents an independent line of development, so if you are working on a separate feature that you want to complete and test before you are ready to share the results of your work and integrate them into master, doing it in a feature branch is the best solution. This way you can make sure unstable code is not committed to the main code base of your project, and you can easily switch to other tasks if necessary.

Pros:

  • As opposed to using changelists to group changes, using feature branches is safe. After you've committed changes to Git, they become part of Git project history, so you can always restore your commit through Git reflog even if you corrupt your working tree. After you've pushed your changes, they are backed up.
  • You can develop parallel non-related features and test them atomically.
  • When you've finished development in your branch, you can reorder or squash commits, so that your history is linear and clean.
  • It is easy to collaborate on your feature, or develop it from different machines.

Cons:

  • It can take time to switch branches on really large projects.
  • It's not very convenient to test related features together.
  • You have to learn a workflow for using feature branches and integrating your changes into the main code base.

There are two major approaches for using feature branches and integrating your changes into the main code base:

Use merge to integrate changes from a feature branch

The major benefit of the merge option is full traceability, as commits merged into the main code base preserve their original hash and author, and all commits that are part of one feature can be grouped together.

This workflow is good for projects where committing changes to the main code base involves pull requests or an hierarchical approval procedure, as existing branches are not changed in any way.

The main drawback of this approach is that extraneous merge commits are created each time you need to incorporate changes, which intensely pollutes project history and makes it difficult to read.

The merge option workflow involves the following steps:

  1. Create a branch for your separate line of development.
  2. Commit your changes while you develop.
  3. Push your branch to a remote repository. This should be done for backup, and so that you can collaborate or work from different machines.
  4. Switch to a different branch when you need to perform work that is not related to your feature.
  5. Have your feature reviewed and tested, and make the necessary fixes.
  6. When you are ready to integrate the results of your work into the main branch (e.g. master), do the following:

Use rebase to integrate changes from a feature branch

The major benefit of this option is that you get a clean project history that is easy for others to read and understand. Your log does not contain unnecessary merge commits produced by the merge operation, and you get linear history that is easy to navigate and search through.

When deciding to adopt this workflow, you should keep in mind, however, that rebase rewrites project history as it creates new commits for each commit in the original feature branch, so they will have different hashes, which obstructs traceability.

The rebase option involves the following steps:

  1. Create a branch for your separate line of development.
  2. Commit your changes often while you develop.
  3. Push your branch to a remote repository. This should be done for backup, and so that you can collaborate or work from different machines.
  4. Rebase your feature branch onto master from time to time. It only makes sense to do this if your feature branch is a long one. This is useful to:
    • make sure your feature branch and master do not fall too far apart.
    • avoid resolving numerous conflicts when you finally integrate your changes into the main code base. When you rebase regularly, you can resolve conflicts iteratively and do not end up struggling with a long diff.
    • speed up checking out branches, as switching between branches gets slower as soon as they diverge sufficiently.

    Rebasing involves the following steps:

    • Fetch changes from the remote, or pull changes into the master branch.
    • Rebase your branch onto master.
    • Force push the results of the rebase operation to your feature branch.

  5. Switch to master when you need to perform work that is not related to your feature. When you turn back to your feature branch, perform Checkout with rebase.
  6. Have your feature reviewed and tested, and make the necessary fixes.
  7. Perform Interactive Rebase when your feature has been completed. This allows you to reorder and squash commits to make your feature branch history look nice and clean.
  8. When you are ready to integrate the results of your work into the main branch (e.g. master), do the following:
    • Checkout the master branch.
    • Merge your branch with master. Since master has not diverged, Git will just move the pointer forward to the latest commit of the feature branch instead of creating a new merge commit (this is referred to as a fast-forward merge).
    • Delete the feature branch.
    • Push.
Last modified: 29 November 2017