IntelliJ IDEA: Resolving Merge Conflicts in Git
Aug 15, 2023
IntelliJ IDEA: Resolving Merge Conflicts in Git
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, 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 this screencast, we’re going to take a look at resolving merge conflicts. For more information, take a look at: https://jb.gg/waysas *Author: Marit van Dijk Download IntelliJ IDEA: https://jb.gg/download-intellij-idea Join us: Website https://jb.gg/website Blog https://jb.gg/blog Twitter https://twitter.com/intellijidea Facebook https://www.facebook.com/IntelliJIDEA/ #intelliJIDEA #intelliJ #jetbrains #java #programming
Content
3.92 -> In this screencast we're going to take
a look at resolving merge conflicts.
9.04 -> Let's open the git window using Command +
9 on Mac (or Alt + 9 on Windows and Linux).
15.68 -> Merge conflicts may arise for example when we are
working on a feature branch while other changes
21.12 -> have been applied to the main branch. As we can
see, we are currently on the main branch. We want
26.8 -> to merge the feature branch into main. We can do
so by clicking the feature branch in the git log
32.64 -> and selecting Branch 'feature'
> Merge 'feature' into 'main'.
37.44 -> When we do so, IntelliJ IDEA opens a popup telling
41.44 -> us there are Conflicts, as we
can also see in the file itself.
42.72 -> The Conflicts dialog offers us several options:
46.64 -> the option to Accept yours which will
apply the changes on the current branch
51.12 -> (main) and discard the changes from
the other branch (the feature branch)
55.92 -> the option Accept theirs which will apply
the changes from the other branch (so, the
60.64 -> feature branch) and discard the changes from main.
If you are unsure about which is "yours" and which
67.44 -> is "theirs", note that these are also visible
in the table to the left of the buttons and the
72.88 -> branch names are mentioned in brackets. We see
that both branches have been Modified. Choosing
79.92 -> either "Accept yours" or "Accept theirs" in this
case would discard the changes made on the feature
85.2 -> or main branch respectively. Only choose those
options if you want to discard those changes.
91.76 -> the third option here is Merge which
we will take a look at in a second.
96.16 -> Note there is a Close button at the bottom.
99.04 -> When we click Close in the Conflicts dialog, we
see a notice "feature was Merged with Conflict".
104.64 -> We can click Resolve in this notice
to reopen the Conflicts dialog.
109.28 -> Alternatively, when we click the Close
button, a Merge Conflicts node will appear
114.24 -> in the Local Changes view. We can open
the Commit tab using Command + 0 on Mac
120.32 -> (or Alt + 0 on Windows and Linux). When we open
the Changes by clicking on the arrow > we see
126.32 -> the Merge Conflicts node. We can click the
Resolve link to reopen the Conflicts dialog.
131.6 -> Now you know how to reopen the conflicts
dialog if you accidentally close it.
136.8 -> Because there are changes on both branches that we
want to keep, we will use Merge. When we click the
142.16 -> Merge button, the Merge Revisions dialog opens.
Here we can see the changes to our current branch
148.24 -> (main) on the left, and the changes that we want
to merge from the feature branch on the right.
154.16 -> There is a fully-functional editor in the middle.
156.88 -> We see that there are several changes:
159.6 -> a removed line is shown in grey
lines that were added are shown in green
165.2 -> lines that were changed are shown in
blue; the changes made are highlighted
171.12 -> conflicts are shown in red
For each change we can decide
175.6 -> to Ignore it by clicking the X or to
accept it by clicking the arrows >>.
180.8 -> The first change is a removed line. We
can Ignore this change, by clicking the X.
186.56 -> We will see that the line is not
removed from the middle pane.
190.56 -> This is a mistake however, because we no longer
need this import. We can use Command + Z on Mac
197.28 -> (or Control + Z on Windows / Linux) to undo
this mistake. Instead, let's accept this
203.52 -> change by the arrows >>, and we'll see that
the line is removed from the middle pane.
207.84 -> Depending on the number of changes,
209.68 -> Accepting or Ignoring each change
individual change might take some time.
215.44 -> We can automatically merge all non-conflicting
changes from the toolbar using Apply All
221.6 -> Non-Conflicting Changes. Alternatively, we can use
Apply Non-Conflicting Changes from the Left Side
228.08 -> or Apply Non-Conflicting Changes from the Right
Side to merge non-conflicting changes from the
232.64 -> left/right parts of the dialog respectively.
Let's accept all. We see that the Left/All/Right
241.04 -> buttons are now greyed out as there are
no more non-conflicting changes to apply.
245.44 -> Now we are down to the remaining conflict.
247.68 -> Another way of resolving conflicts is by
right-clicking a highlighted conflict in
252 -> the central pane and use the commands
from the context menu. When we Accept
257.12 -> either the left or the right that change will
be applied and the change on the other side
262.08 -> will remain open. When we choose Resolve using
Left and Resolve using Right the changes from
268 -> either the left or right will be applied and
the changes from the other side will be ignored.
273.2 -> In this example, we want to combine the changes
from both sides, so we need to accept them both.
278.96 -> At this point, IntelliJ IDEA has concluded
that all changes have been processed,
283.52 -> so we can choose to Save changes and finish
merging which will close this dialog.
288.88 -> However, as we can see from the error
highlighting this doesn't look quite right.
293.44 -> The reason here is that both branches have added
fields to this record, so we need to fix these
298.96 -> brackets here. It's a good thing the middle pane
is a fully-functional editor, so we can edit this
304.16 -> out. There, that looks much better. We can click
Apply and are done resolving our merge conflicts.
310.88 -> Thank you for watching!
Source: https://www.youtube.com/watch?v=WgipWkaU2MM