|
| 1 | +Solving Git Conflicts |
| 2 | +===================== |
| 3 | + |
| 4 | +What Are Git Conflicts? |
| 5 | +----------------------- |
| 6 | + |
| 7 | +Git conflicts occur when Git is unable to automatically merge changes made to the \ |
| 8 | +same lines of code or when changes overlap in a file across branches. |
| 9 | +This often occurs in RocketPy repository when two developers make changes to the same \ |
| 10 | +file and try to merge them. |
| 11 | +In this scenario, Git pauses the operation and marks the conflicting files, \ |
| 12 | +requiring manual intervention. |
| 13 | + |
| 14 | +Merge vs Rebase |
| 15 | +--------------- |
| 16 | + |
| 17 | +**Merge** |
| 18 | + |
| 19 | +The ``merge`` operation combines the changes from one branch into another. \ |
| 20 | +It creates a new commit that marks the merging of the two branches together. |
| 21 | + |
| 22 | +* Retains the full history of both branches. |
| 23 | +* Non-linear history shows how changes were combined. |
| 24 | +* The history can become cluttered if there are many branches, due to the number of merge commits. |
| 25 | + |
| 26 | +**Rebase** |
| 27 | + |
| 28 | +The ``rebase`` operation integrates changes from one branch into another by reapplying \ |
| 29 | +the commits on top of the target branch. It results in a more linear history. |
| 30 | + |
| 31 | +* Produces a cleaner, linear commit history. |
| 32 | +* Easier to follow the sequence of changes. |
| 33 | +* Can rewrite history, leading to potential issues when working on shared branches. |
| 34 | + |
| 35 | +Example Commands |
| 36 | +---------------- |
| 37 | + |
| 38 | +Let's consider a common scenario from which conflicts can arise: |
| 39 | +updating a feature branch with the latest changes from the branch it was created. |
| 40 | +Both ``merge`` and ``rebase`` can be used to update the feature branch. |
| 41 | +However, the ``rebase`` option is highly recommended to keep a more linear history. |
| 42 | + |
| 43 | +Merge |
| 44 | +~~~~~ |
| 45 | + |
| 46 | +Suppose you have two branches, ``enh/feature`` that was branched off ``develop``. |
| 47 | +It is likely that ``develop`` has been updated since you branched off ``enh/feature``, \ |
| 48 | +therefore before merging ``enh/feature`` into ``develop``, you should update ``enh/feature`` \ |
| 49 | +with the latest changes from ``develop``. |
| 50 | + |
| 51 | +One way to do this is by merging ``develop`` into ``enh/feature`` as follows: |
| 52 | + |
| 53 | +.. code-block:: console |
| 54 | +
|
| 55 | + git checkout develop |
| 56 | + git pull |
| 57 | + git checkout enh/feature |
| 58 | + git merge develop |
| 59 | +
|
| 60 | +If there are conflicts, Git will pause the operation and mark the conflicting files. \ |
| 61 | +VS Code provides a visual interface to resolve these conflicts in the **Merge Editor**. |
| 62 | +If you want to abort the merge, you can use ``git merge --abort``. |
| 63 | + |
| 64 | +Rebase |
| 65 | +~~~~~~ |
| 66 | + |
| 67 | +Similarly, another way to update ``enh/feature`` with the latest changes from ``develop`` \ |
| 68 | +is by rebasing ``enh/feature`` onto ``develop`` as follows: |
| 69 | + |
| 70 | +.. code-block:: console |
| 71 | +
|
| 72 | + git checkout develop |
| 73 | + git pull |
| 74 | + git checkout enh/feature |
| 75 | + git rebase develop |
| 76 | +
|
| 77 | +Differently from merge, if there are conflicts, they must be resolved commit by commit. \ |
| 78 | +After resolving each step conflicts, you can continue the rebase with ``git rebase --continue``. \ |
| 79 | +If you want to abort the rebase, you can use ``git rebase --abort``. |
| 80 | + |
| 81 | +When to Use Merge or Rebase |
| 82 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 83 | + |
| 84 | +Generally, the maintainers will inform you which operation to use when merging your PR. \ |
| 85 | +Notice that there is no wrong way to merge branches, but ``rebase`` is usually preferred \ |
| 86 | +to keep a more linear history. |
| 87 | + |
| 88 | +Furthermore, it is important to avoid letting conflicts accumulate, since they can become \ |
| 89 | +harder to resolve. It is recommended to update your feature branch frequently with the latest \ |
| 90 | +changes from the branch it was branched off. |
| 91 | + |
| 92 | +Solving Conflicts |
| 93 | +----------------- |
| 94 | + |
| 95 | +When conflicts arise, Git marks the conflicting files. The file will contain markers like: |
| 96 | + |
| 97 | +.. code-block:: console |
| 98 | +
|
| 99 | + <<<<<<< HEAD |
| 100 | + Current branch changes |
| 101 | + ======= |
| 102 | + Incoming branch changes |
| 103 | + >>>>>>> branch-name |
| 104 | +
|
| 105 | +The ``HEAD`` section contains the changes from the current branch, while the ``branch-name`` section \ |
| 106 | +contains the changes from the incoming branch. |
| 107 | +The ``=======`` line separates the two sections. |
| 108 | +One can manually edit the file to resolve the conflict, removing the markers and keeping the desired changes, however \ |
| 109 | +for convenience it is recommended to use a visual tool like the *Merge Editor* in VS Code. |
| 110 | + |
| 111 | +Resolving Conflicts in VS Code |
| 112 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 113 | + |
| 114 | +When a conflict occurs, VS Code will open the *Merge Editor* to help you resolve it.\ |
| 115 | + |
| 116 | +1. Open the conflicting file (marked with a ``!``). |
| 117 | +2. The *Merge Editor* will show the conflicting sections side by side. |
| 118 | +3. Click on the ``Accept Current Change`` or ``Accept Incoming Change`` buttons to keep the desired changes, sometimes both changes will be kept or even a manual edit will be necessary. |
| 119 | + |
| 120 | +More details on VS Code interface and conflict solver can be found in `VS Code Docs <https://code.visualstudio.com/docs/sourcecontrol/overview#_3way-merge-editor>`_. |
| 121 | +After resolving the conflicts, save the files, make sure all conflicts are resolved, and then \ |
| 122 | +commit the changes. |
| 123 | + |
0 commit comments