IntelliJ IDEA 2017.3 Help

Applying Patches

Postponed changes stored in a patch file can be applied to the target file or directory later. If the source code was edited after creating the patch, conflicts may arise. IntelliJ IDEA suggests a handy way to resolve such conflicts and merge the patch with the changes.

When a patch is opened, IntelliJ IDEA detects files with the same name as the modified files. For each detected file, IntelliJ IDEA compares the path to it relative to the base directory, with the path from the patch, and chooses the closest match. If no matching path is found, the file is considered to be located in the project base directory and is highlighted in red.

You can apply changes to files stored in different locations from those specified in the patch by mapping an arbitrary directory as the base one, or stripping off the leading directories.

To apply a patch

  1. On the main menu, choose VCS | Apply patch.
  2. In the Apply Patch dialog box that opens, specify the fully qualified name of the patch file. Type the name manually or click the Browse button browseButton.png and locate the desired patch file in the Select Patch File dialog box.

    You can also drag and drop a file or an email attachment to the Apply Patch dialog, and it will be selected automatically.

  3. Configure the patch presentation layout. To have changes shown in a flat view, press the Group by Directory toolbar button vcsToolbarGroupByDirIcon.
    Release the button to have changes shown in a directory tree view.
  4. To have a change applied, select the checkbox next to it.
  5. To have a change applied to a modified file that has been moved to another location, specify the new file location.
    • To map another base directory, select the desired file, directory, or a group files/directories and click the Map base directory toolbar button icon vcs basic directory. In the dialog that opens choose the directory relative to which file names will be interpreted.
    • To remove leading directories from the path, click the Strip Directory toolbar button icon vcs strip directory as many times as many leading directories you need to strip. The number of removed slashes is indicated in square brackets.
    • To revert the last strip directory action, click the Restore Directory toolbar button icon vcs restore directory. Click the button as many times as many previously stripped leading directories you need to restore.
    • To revert all the strip directory actions in the selection, click the Reset Directories toolbar button icon vcs reset directories.
    • To have all the leading directories stripped and have the changes applied to the file with the specified name in the base directory, click the Remove Directories toolbar button icon vcs remove directories.
  6. To view the differences and possible conflicts between your local working copy, the repository version, and the patch in the Differences Viewer for Files, select the desired change and click the Show Differences button vcsToolbarCompareIcon.gif.
  7. To resolve conflicts between the patched and the current versions, if any, in both versions select the changes to be merged to the resulting file, and then click Apply.

    Note that you apply a patch that contains a lot of files and causes numerous conflicts, you can cancel applying the patch by clicking Abort, and then select whether you want to abort, skip or cancel the remaining conflicts.

You can also copy a patch file content and apply it by choosing VCS | Apply Patch from Clipboard from the main menu. This is convenient when you receive a patch by email, for example, and do not want to save it. For Git format patches, IntelliJ IDEA extracts the commit message and the author and automatically fills the corresponding fields in the Commit Changes dialog.

Last modified: 6 March 2018

See Also