Как переименовать проект в pycharm
Перейти к содержимому

Как переименовать проект в pycharm

  • автор:

Rename refactorings

Use the Rename refactoring to change names of symbols, files, variables, functions, packages, modules and all the references to them throughout code.

Renaming local variables or functions can be done easily inline since only the limited scope is affected. Renaming classes or their methods could potentially impact a lot of files. Preview potential changes before you refactor.

When you rename methods that are declared in abstract base classes, you can also review and rename their implementations as well. The refactoring changes are displayed on the Refactoring Preview tab in the Find tool window.

Rename a code element inline

  1. Place the caret at the element that you want to rename and press Shift+F6 . If there are occurrences of this element in comments, strings, or text files, inline icons are displayed: Inline icons of renaming options
  2. Click the icons and select the desired options: Options for inline rename refactoringTo open the Rename dialog, click More options or press Shift+F6 again.
  3. Type the new name of the element. All occurrences will change automatically:

Use a quick-fix or a gutter icon

  1. In the editor start renaming a code element. PyCharm will display in the gutter.
  2. Click the gutter icon or press Alt+Enter and apply a suggestion. Rename inlinePyCharm renames the code element and updates its usages accordingly. Rename refactoring result

Rename a directory or a module

  1. In the Project tool window right-click a directory or a module that you want to rename.
  2. From the context menu, select Refactor | Rename ( Shift+F6 ).
  3. In the Rename dialog that opens, type the new name, specify additional options and the scope of the refactoring, and click Refactor .

Use the Rename dialog

The Rename dialog

  1. In the editor, select the element you want to rename. If you need to rename a file, select one in the Project tool window.
  2. Press Shift+F6 or go to Refactor | Rename in the main menu.
  3. You can perform the rename refactoring inline or press Shift+F6 again to open the Rename dialog. Enter a new name of the element to enable the Preview and Refactor buttons. You can specify additional options. For example, specify where to search for element occurrences, or what else to rename. You can also specify a scope for the refactoring.
  4. Click Preview to see the potential changes or click Refactor . When you click Preview , PyCharm opens the Find tool window with the results of found usages where you can check the results and confirm the refactoring ( Do Refactor ).

Next time you invoke the Rename refactoring, PyCharm remembers the options you have specified inside the Rename dialog.

You cannot rename the .idea project directory since PyCharm always reads the project files from the directory with that exact name. Also you cannot rename any element that is defined outside of the project, for example, in an imported package.

If you need to edit variables rather than to change them completely, go to Settings | Editor | Code Editing and clear the Preselect current symbol name for Rename refactoring checkbox located in the Refactorings section.

Rename projects

In most cases, the name of a PyCharm project is the same as the name of its root folder, so the easiest way to change the name of a project is just rename its root folder.

  1. Right-click the root folder of your project and select Refactor | Rename from the context menu or press Shift+F6 .
  2. In the dialog that opens, choose the rename strategy.
    • If the project name is the same as the name of its root folder, select Rename directory . Renaming a directoryPyCharm will perform the Rename refactoring so that all the references to the directory throughout the code remain valid. Preview of the rename refactoring
    • If the project name is different from the name of its root folder, select Rename project . Renaming a projectAlternatively, select File | Rename Project from the main menu and type the new name of the project in the dialog that opens. Also select this option if your application is deployed to a remote server and the project root folder is mapped to the server root.

Last modified: 07 September 2023

Edit Git project history

Git allows you to edit your project history. This is useful when you’re working on a feature branch and want to clean it up and make it look the way you want before you share it with others. For example, you can edit commit messages, squash together smaller commits related to the same functionality, or split a commit that contains unrelated changes into separate commits, add changes to a previous commit, and so on.

Avoid modifying the history for a remote branch with multiple contributors unless absolutely necessary, for example, if you accidentally pushed some sensitive data.

Pushing modifications that rewrite a branch history to the remote repository will be rejected to prevent data loss, so you will have to force push your changes.

You cannot modify the history for protected branches where push —force is not allowed (configure protected branches in the Version Control | Git page of the IDE settings Control+Alt+S . Note that if a branch is marked as protected on GitHub, PyCharm will automatically mark it as protected when you check it out.

Also, you cannot perform actions that modify a branch history for commits that are not contained in the branch currently checked out.

Edit a commit message

If the only thing you need to change is a commit message, you can edit it before you push this commit.

  1. Right-click the commit whose message you want to edit in the Log tab of the Git tool window Alt+9 and select Edit Commit Message from the context menu, or press F2 .
  2. In the dialog that opens, enter a new commit message and click OK .

Amend the previous commit

Sometimes you may commit too early and forget to add some files, or notice an error in the last commit that you want to fix without creating a separate commit.

You can do this by using the Amend commit option that appends staged changes to the previous commit. As a result, you end up with a single commit instead of two different ones.

  1. In the Commit tool window Alt+0 , select the modified files containing the changes you want to add to the previous commit.
  2. Select the Amend checkbox so that the Commit button changes to Amend Commit and click it.

Amend any earlier commit

If you need to add changes to any earlier commit instead of committing them separately, you can do this by using the fixup or the squash action. Both commands append staged changes to the selected commit, but handle commit messages differently:

  • squash adds the new commit message to the original commit
  • fixup discards the new commit message, leaving only the message from the original commit

Both commands require a rebase because they change the commit hashes.

  1. In the Commit tool window Alt+0 , select the modified files containing the changes you want to append to an earlier commit.
  2. In the Log tab of the Git tool window Alt+9 , right-click the commit that you want to modify with your local changes and select Fixup or Squash Into from the context menu.
  3. Modify the commit message if you’ve chosen to squash changes.
  4. Click the arrow on the Commit button and choose Commit and Rebase .

Squash commits

If you need to meld any two commits related to the same functionality, you can squash them into one for the sake of cleaner branch history.

  1. In the Log tab of the Git tool window Alt+9 select the commits that you want to combine into one and choose Squash Commits from the context menu.
  2. In the dialog that opens, edit the commit message (by default, it contains the messages from both commits) and click OK .
  3. Push Control+Shift+K the changes to the remote branch.

Drop a commit

You can discard a pushed commit in the current branch without creating an additional commit that reverts the changes.

  • Select a commit you want to discard in the Log view and choose Drop Commit from the context menu.

Edit project history by performing interactive rebase

With Git integration in PyCharm, you can edit project history for the sake of making it linear and meaningful by performing interactive rebase . This allows you to clean up the history of commits by altering individual commits, changing their order, squashing commits into one, skipping commits that contain extraneous changes, and so on before you integrate changes from your feature branch to another branch.

Edit the history of the current branch

PyCharm allows you to edit the commits history in the current branch before you apply the changes to a different branch.

  1. Open the Git tool window Alt+9 and switch to the Log tab.
  2. Filter the log so that it only displays commits from the current branch: Filter log by branch
  3. Select the oldest commit in the series of commits that you want to edit, right-click it and choose Interactively Rebase from Here . The Interactive Rebase dialog will be displayed containing the list of all commits in the current branch that were made after the selected commit: Interactive Rebase dialogIf the Interactively Rebase from Here option is disabled, this may be due to one of the following reasons:
    • the selected commit has several parents
    • the selected commit is not in the current branch
    • the selected commit is pushed to a protected branch

To identify the reason, hover over the action in the context menu and look for the message in the status bar:

Status bar message

  • You can perform the following changes to the branch history:
    • Change the order in which commits should be applied: use the and buttons to move commits up and down the list.
    • Pick a commit : this is the default state for all commits. If you need to undo an action you’ve already taken on a commit, click Pick so that this commit is applied as is.
    • Edit : click Stop to Edit so that when you start the rebase, you stop at this commit to be able to edit it. When rebase is stopped at a commit, a notification pops up in the bottom-right corner of the PyCharm window letting you continue or abort the rebase: the rebase status notificationYou can modify this commit using the context actions (such as Revert , Undo , Amend , and so on before continuing the rebase. If you don’t perform any actions, this commit will be applied as is. If you’ve closed the notification, from the main menu choose Git | Continue rebase to resume it.
    • Reword the commit message : click Reword or double-click a commit and edit the text in the mini-editor that opens.
    • Combine two commits into one : select the commit you want to meld into the previous one and click Squash or the arrow next to the Squash button and then Fixup . If you click Squash , by default the messages from the two commits will be combined, so if you don’t modify the resulting commit message this action will be reflected in the branch history. If you click Fixup , the commit message of the fixup commit will be discarded, so this change will be invisible in the branch history. In both cases, you will be able to edit the commit message in the mini editor that opens when you apply one of these actions.
    • Ignore a commit : click Drop so that the changes from the selected commit are not applied.
    • Undo all changes : click Reset to discard all actions you’ve applied to the commits.

    As a result, the Rebasing Commits dialog shows a graph illustrating all actions you’ve applied to commits in your branch, so that you can review them before starting the rebase:

    Interactive Rebase graph

  • Click Start Rebasing .
  • Edit a branch history and integrate it into another branch

    PyCharm allows you to rebase a branch on top of another branch and edit the source branch history before you apply the changes.

    1. From the main menu select Git | Rebase : Git rebase dialog
    2. Click Modify options and choose —interactive .
    3. From the list, select the target branch onto which you want to rebase the current branch: Choose target branch in the Git rebase dialog
    4. If you need to rebase the source branch starting from a particular commit instead of rebasing the entire branch, click Modify options and choose —onto . In the source branch field, enter the hash of the commit starting from which you want to apply the current branch to the new base: Specify commit hash with --ontoTo copy a commit hash, select it in the Log , right-click it and choose Copy Revision Number .
    5. If the branch you want to rebase is not currently checked out, click Modify options , click Select another branch to rebase , and choose a branch from the list that appears: Choose the branch you want to rebasePyCharm will check out this branch before starting the rebase operation.
    6. If you want to rebase all commits reachable in the branch, click Modify options and choose —root (for more information on this option, refer to git-rebase).
    7. If you need to keep empty commits, which are commits that do not change anything from their parent, click Modify options and choose —keep-empty (for more information on this option, refer to git-rebase).
    8. Click Rebase . The Interactive Rebase dialog will be displayed containing the list of all commits in the current branch that were made after the selected commit. Interactive Rebase dialog
    9. You can perform the following changes to the branch history:
      • Change the order in which commits should be applied: use the and buttons to move commits up and down the list.
      • Pick a commit : this is the default state for all commits. If you need to undo an action you’ve already taken on a commit, click Pick so that this commit is applied as is.
      • Edit : click Stop to Edit so that when you start the rebase, you stop at this commit to be able to edit it. When rebase is stopped at a commit, a notification pops up in the bottom-right corner of the PyCharm window letting you continue or abort the rebase: the rebase status notificationYou can modify this commit using the context actions (such as Revert , Undo , Amend , and so on before continuing the rebase. If you don’t perform any actions, this commit will be applied as is. If you’ve closed the notification, from the main menu choose Git | Continue rebase to resume it.
      • Reword the commit message : click Reword or double-click a commit and edit the text in the mini-editor that opens.
      • Combine two commits into one : select the commit you want to meld into the previous one and click Squash or the arrow next to the Squash button and then Fixup . If you click Squash , by default the messages from the two commits will be combined, so if you don’t modify the resulting commit message this action will be reflected in the branch history. If you click Fixup , the commit message of the fixup commit will be discarded, so this change will be invisible in the branch history. In both cases, you will be able to edit the commit message in the mini editor that opens when you apply one of these actions.
      • Ignore a commit : click Drop so that the changes from the selected commit are not applied.
      • Undo all changes : click Reset to discard all actions you’ve applied to the commits.

    As a result, the Rebasing Commits dialog shows a graph illustrating all actions you’ve applied to commits in your branch, so that you can review them before starting the rebase:

    Interactive Rebase graph

  • Click Start Rebasing .
  • Find and replace in project

    You can search for a text string within a project, use different scopes to narrow your search process, exclude certain items from your search, find usages and occurrences.

    Find the search string in a project

    Find in Files

    1. Go to Edit | Find | Find in Files Control+Shift+F .
    2. In the search field, type your search string. Alternatively, in the editor, highlight the string you want to find and press Control+Shift+F . PyCharm places the highlighted string into the search field. To see a list of your previous searches, press Alt+ArrowDown . If you need, specify the additional options. PyCharm lists the search strings and the files that contain them. If the search string is found several times on the same line of code, PyCharm merges the results in one line. To do a multi-line search, click the icon to enter a new line, and press Control+Alt+ArrowDown / Control+Alt+ArrowUp to browse through occurrences.
    3. Check the results in the preview area of the dialog where you can replace the search string or select another string, press Control+Shift+F again and start a new search.
    4. To see the list of occurrences in a separate tool window, click Open in Find Window . Use this window and its options to group the results, preview them, and work with them further. If you want to see each new search result in a separate tab in the Find tool window, click on the bottom of the Find in Files dialog and select the Open Results in New Tab checkbox.

    Copy paths or references of the found files

    1. In the list of search results, right-click the result for which you want to copy a path and click Copy/Reference .
    2. In the Copy window, select the path or reference you need.

    Narrow your search

    You can use different options in the Find in Files dialog to adjust your search process.

    Search in class hierarchy

    • Select options such as Words () or Match case () to find the exact word in a project or match the letter case.
    • With selected, PyCharm automatically escapes special regex symbols with backslash \ when you search for a text string that contains them. Keep in mind that if you copy ( Control+C ) the string first and then paste ( Control+V ) it in the search field, the regex symbols will not be taken into account. For more information about regex , refer to the search with regex documentation.
    • Click the icon to filter your search. For example, you can filter the search to omit comments or search only in comments instead.
    • Select one of the displayed options such as Module or Directory to limit your search. Moreover, you can select the Scope option that offers you a list of predefined scopes for your search. For example, you can limit your search only to the open files in your project. If you work without tabs, the scope Recently Viewed Files or Recently Changed Files option might become quite useful. You can also create your own custom scope, click the Browse icon () to open the Scopes dialog.

    Search in the specific file types

    Use the File Mask option to narrow your search to a specific file type. You can select the existing file type from the list, add a new file type, or add an additional file mask syntax to search for file types with certain patterns.

    File mask

    1. Go to Edit | Find | Find in Files Control+Shift+F .
    2. In the Find in Files dialog, select the File Mask checkbox and from the list of file types, select the one you need. PyCharm limits its search to the specified type.
    3. If you don’t find the file type you need in the list, enter your file type in the File Mask field. Besides * , other wildcards are supported. If necessary, specify several file types using commas as separators. You can manually add a file mask in the search field. If necessary, specify several file types separating them with commas.

    Search for usages in a project

    You can search for usages of a symbol in your whole project or in a scope that you set.

    Click to open the Find Usages dialog.

    1. Go to Edit | Find | Find in Files Control+Shift+F .
    2. Select the symbol for which you want to find usages. Note that you can extend your search to file usages as well.
    3. In the main menu, go to Edit | Find | Find Usages Alt+F7 . To see usages of the selected symbol within a file, right-click the context menu in your file and select Find Usages Control+F7 .
    4. Check the results in the Find tool window. Click to open the Find Usages dialog. Find tool windowYou can also pull out the results from the previous Find Usages actions. Go to Edit | Find | Recent Find Usages and the usage query. While in the Find tool window, you can use the Preview area to check the places where the usages were found, to see a call hierarchy for methods, data flow for fields, and so on. Find tool window preview areaIf PyCharm doesn’t return any results, it will display a message suggesting to opt for more options. You can follow the link or press Control+Alt+Shift+F7 to open the Find Usages dialog where you can set a new scope for your search. Find usage dialogFor example, you can set your search scope to production files only or to only open files. To set a custom scope, click .
    5. When you are done setting a new scope, click Find .

    If you want PyCharm to show you usages of the selected symbol in the separate window, press Control+Alt+F7 . You can use this window for quick navigation.

    Find usage dialog

    Press the same shortcut again to see the usages in the default scope.

    To view the results of previous Find Usages actions, go to Edit | Find | Recent Find Usages in the main menu, and then select the usage query.

    Disable automatic highlighting of usages

    When you place the caret at a symbol, the IDE highlights all usages of this symbol in the current file. Use the F3 and Shift+F3 shortcuts to navigate between highlighted symbols.

    In the Power Save Mode, highlighting of usages is disabled.

    If necessary, you can disable the automatic highlighting.

    • In the Settings dialog ( Control+Alt+S ), go to Editor | General , and clear the Highlight usages of element at caret checkbox.

    When the automatic highlighting is disabled, place the caret at the necessary symbol and press Control+Shift+F7 . This will highlight all usages of the symbol in the current file.

    Replace the search string in a project

    Replace in path dialog

    1. Press Control+Shift+R ( Edit | Find | Replace in Path ) to open the Replace in Path dialog.
    2. In the top field, enter your search string. In the bottom field, enter your replacement string. For example, if you want to replace a variable name with a new name for a large project, use the Replace in path instead of the Rename refactoring since your variable can appear in the config files as well.
    3. Click one of the available Replace commands.

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *