CLion 2017.3 Help

Use patches

Instead of committing your changes, you can put them in a .patch file that you can apply to your sources later, or send by email, etc.

Create a patch

To create a patch based on uncommitted changes, do the following:

  1. Open the Version Control tool window (Alt+9) and switch to the Local Changes tab.
  2. Select a file or a changelist based on which you want to create a patch.
  3. Select Create Patch from the context menu or from the main VCS menu.
  4. In the dialog that opens, make sure that all changes you want to include in the patch are selected, enter a commit comment (optionally) and click Create Patch.
  5. In the Patch File Settings dialog, modify the default patch file location if necessary, and click OK.

If you do not need to save a patch to a file, and want, for example, to send it by email or through a messenger, you can select Copy as Patch to Clipboard from the context menu of the selected file or changelist, and then paste it to a message body.

You can also create a patch based on changes that have already been committed. To create a patch from an entire commit, locate it in the Log view, and select Create Patch from the context menu. If you need to create a patch on a single file, and the corresponding commit contains multiple files, do the following:

  1. Select the required file in any view (in the Project tool window, in the editor, in the Local Changes tab of the Version Control tool window, etc.).
  2. Select Git | Show History from the main VCS menu or from the context menu of the selection. The History tab is added to the Version Control tool window.
  3. Right-click a revision and choose Create Patch from the context menu, or click the Create Patch icon iconCreatePatch on the toolbar.

Apply a patch

To apply changes stored in a file to your sources, do the following:

  1. Select VCS | Apply patch from the main menu.
  2. In the Apply Patch dialog that opens, specify the path to the .patch file you want to apply.
  3. If necessary, click the Map base directory icon icon vcs basic directory to specify a directory relative to which file names in the patch file will be interpreted. You can map a base directory to a single file, directory, or to a selection.
  4. If the source code was edited after a patch was created, conflicts may arise. To check if you patch can be applied without conflicts, click the Show Diff icon icon showDiff or press Ctrl+D. If there are conflicts, the corresponding lines will be highlighted in red.
  5. If you want to apply changes to files stored in different locations from those specified in the patch, you can strip off the leading directories by using the Strip Directory button icon vcs strip directory.
  6. Select an existing changelist where you want to add the patch from the drop-down list, or specify the name of a new changelist in the Name field, and, optionally, enter a comment to this changelist.
  7. If you want to make this changelist active, select the Set active option.
  8. If you want CLion to preserve the context of a task associated with the new changelist on its deactivation and restore the context when the changelist becomes active, select the Track context option (see Managing Tasks and Contexts for details).

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, CLion extracts the commit message and the author and automatically fills the corresponding fields in the Commit Changes dialog.

Last modified: 15 December 2017