When you work in a team, you may come across a situation when somebody pushes changes to a file you are currently working on. If these changes do not overlap (i.e. changes were made to different lines of code), the conflicting files are merged automatically. However, if the same lines were affected, Git cannot randomly pick one side over the other, and asks you to resolve the conflict.
In Git, conflicts may arise when you attempt to perform one of the following operations: pull, merge, rebase, cherry-pick, unstash changes or apply a patch. If there are conflicts, these operations will fail, and you will be prompted to accept the upstream version, prefer your version, or merge the changes:
WebStorm provides a tool for resolving conflicts locally. This tool consists of three panes. The left pane shows the read-only local copy; the right pane shows the read-only version checked in to the repository. The central pane shows a fully-functional editor where the results of merging and resolving conflicts are displayed. Initially, the contents of this pane are the same as the base revision of the file, that is, the revision from which both conflicting versions are derived.
To resolve conflicts, do the following:
- Click Merge in the Files Merged with Conflicts dialog, or select the conflicting file in the editor and choose VCS | <your_VCS> | Resolve Conflicts from the main menu.
- To automatically merge all non-conflicting changes, click (Apply All Non-Conflicting Changes) on the toolbar. You can also use the (Apply Non-Conflicting Changes from the Left Side) and (Apply Non-Conflicting Changes from the Right Side) to merge non-conflicting changes from the left/right parts of the dialog respectively.
- To resolve a conflict, you need to select which action to apply (accept or ignore ) to the left (local) and the right (repository) version, and check the resulting code in the central pane:
For simple conflicts (for example, if the beginning and the end of the same line have been modified in different file revisions), the Resolve option is available that allows merging the changes in one click:
Such conflicts are not resolved with the Apply All Non-Conflicting Changes action since you must make sure that they are resolved properly.
- It may also be useful to compare different versions to resolve a conflict. Use the toolbar button to invoke the list of options. Note that Base refers to the file version that the local and the repository versions originated from (initially displayed in the middle pane), while Middle refers to the resulting version.
- Review merge results in the central pane and click Apply.
Handle conflicts related to LF and CRLF line endings
Quite often, people working in a team and contributing to the same repository use different operating systems. This may result in problems with line ending, because Unix, Linux and MacOS us
LF, and Windows uses
CRLF to mark the end of a line.
WebStorm displays the discrepancies in line endings in the Differences Viewer, so you can fix them manually. If you want Git to solve such conflicts automatically, you need to set the
core.autocrlf attribute to
true on Windows and to
input on Linux and MacOS (for more details, see Dealing with line endings). You can change the configuration manually by running
git config --global core.autocrlf true on Windows or
git config --global core.autocrlf input on Linux and macOS.
However, WebStorm can automatically analyze your configuration, warn you if you are about to commit
CRLF into a remote repository, and suggest setting the
core.autocrlf setting to
input depending on your operating system.
To enable smart handling of
CRLF line separators, open the Settings dialog (Ctrl+Alt+S), and select the Version Control | Git node on the left. Enable the Warn if CRLF line separators are about to be committed option.
After you have enabled this option, WebStorm will display the Line Separators Warning Dialog each time you are about to commit a file with
CRLF separators, unless you have set any related Git attributes in the affected file (in this case, WebStorm supposes that you clearly understand what you are doing and excludes this file from analysis).
In the Line Separators Warning Dialog, click one of the following:
- Commit As Is to ignore the warning and commit a file with
- Fix and Commit to have the
core.autocrlfattribute set to
inputdepending on your operating system. As a result,
CRLFline separators will be replaced with
LFbefore the commit.
If, at a later time, you need to review how exactly conflicts were resolved during a merge, you can locate the required merge commit in the Log view, select a file with conflicts in the Commit Details pane in the right, and click or press Ctrl+D (see Review how changes were merged for details).