# [[Resolving Git conflicts]] ![[Resolving Git conflicts.svg]] If you use [[Git]] enough, sooner or later you'll run into some sort of conflict. A conflict is when the local version of a repository differs from the remote repository. This can happen when: - Other people are working on the same repo at the same time as you - You made changes on the same repo (even the same branch) in different places, such as different machines or by editing something in the [[GitHub]] UI. Conflicts aren't a bad thing, but they can be tricky to resolve. They also operate at three different levels. ## Branch-level conflicts Branch-level conflicts arise when Branch A and Branch B contain commits that have changed the same files or lines in incompatible ways. There are three strategies to resolve such conflicts. ### Merging Merging means to combine two branches if possible, and requires creating a new commit, called a merge commit, that includes all the changes from both branches. This is good for when the two branches may have touched the same file, but they're not in opposition and you want to just take all those changes and have them coexist. ```bash git checkout main git merge feature ``` ### Rebasing Rebasing means to rewrite your commits so that they appear as if they were based on the other branch. So if you were working on Branch A, and when you tried to push, you found out that there is another Branch B, rebasing means that you take your changes on A and just add them to Branch B, "replaying" them as if you had always committed to B in the first place. If you think your changes haven't been affected by Branch B's changes, you probably just want to rebase. ```bash git checkout feature git rebase main ``` ### Fast-forward Fast-forwarding is when Git just updates the pointer to the tip of your branch. Fast-forwarding is technically not a strategy for resolving a real branch conflict, because you can only fast forward if there has been no divergence. But I'm including it here for completeness because it's another option for merging. Git fast-forwards automatically unless you disable it. ```bash git checkout main git merge feature # becomes fast-forward if possible ``` If you want to explicitly *not* fast-forward, you have to do: ```bash git merge --no-ff feature ``` ## File-level conflicts A file-level conflict is one step lower than the branch-level conflicts. Even when you employ one of those strategies (such as merging, for example), you might still end up with situations where the two branches have touched the same file. Applying file-level conflict resolution strategies prevents you from having to manually edit the files. It's a way of setting what to do with every conflict. See [[Resolving file-level conflicts in Git]]. ## Line conflicts You can always resolve line conflicts by manually going through each one and choosing whether to keep A's changes, B's changes, or something else entirely. %% # Excalidraw Data ## Text Elements ## Drawing ```json { "type": "excalidraw", "version": 2, "source": "https://github.com/zsviczian/obsidian-excalidraw-plugin/releases/tag/2.1.4", "elements": [ { "id": "4y8R7iOA", "type": "text", "x": 118.49495565891266, "y": -333.44393157958984, "width": 3.8599853515625, "height": 24, "angle": 0, "strokeColor": "#1e1e1e", "backgroundColor": "transparent", "fillStyle": "solid", "strokeWidth": 2, "strokeStyle": "solid", "roughness": 1, "opacity": 100, "groupIds": [], "frameId": null, "roundness": null, "seed": 967149026, "version": 2, "versionNonce": 939059582, "isDeleted": true, "boundElements": null, "updated": 1713723615080, "link": null, "locked": false, "text": "", "rawText": "", "fontSize": 20, "fontFamily": 4, "textAlign": "left", "verticalAlign": "top", "containerId": null, "originalText": "", "lineHeight": 1.2 } ], "appState": { "theme": "dark", "viewBackgroundColor": "#ffffff", "currentItemStrokeColor": "#1e1e1e", "currentItemBackgroundColor": "transparent", "currentItemFillStyle": "solid", "currentItemStrokeWidth": 2, "currentItemStrokeStyle": "solid", "currentItemRoughness": 1, "currentItemOpacity": 100, "currentItemFontFamily": 4, "currentItemFontSize": 20, "currentItemTextAlign": "left", "currentItemStartArrowhead": null, "currentItemEndArrowhead": "arrow", "scrollX": 583.2388916015625, "scrollY": 573.6323852539062, "zoom": { "value": 1 }, "currentItemRoundness": "round", "gridSize": null, "gridColor": { "Bold": "#C9C9C9FF", "Regular": "#EDEDEDFF" }, "currentStrokeOptions": null, "previousGridSize": null, "frameRendering": { "enabled": true, "clip": true, "name": true, "outline": true } }, "files": {} } ``` %%