|
| 1 | +## Handling Conflicts |
| 2 | + |
| 3 | +Conflicts are common if main has updates to the files you are working on. In which case, you'll have to run a rebase. |
| 4 | + |
| 5 | +If you are warned of a conflict: |
| 6 | + |
| 7 | +- In VS Code: open the conflicted files and use the built-in conflict resolution. |
| 8 | + |
| 9 | +### 1. See which files are conflicted |
| 10 | +```bash |
| 11 | +git status |
| 12 | +``` |
| 13 | + |
| 14 | +### 2. Resolve conflicts manually |
| 15 | +You will see sections like: |
| 16 | + |
| 17 | +```text |
| 18 | +<<<<<<< HEAD |
| 19 | +code from main |
| 20 | +======= |
| 21 | +your branch’s code |
| 22 | +>>>>>>> mybranch |
| 23 | +``` |
| 24 | + |
| 25 | +1. Decide what the final code should be. |
| 26 | + |
| 27 | +2. Sometimes keep main, sometimes your branch, sometimes both. |
| 28 | + |
| 29 | +### 3. Resolving conflicts in VS Code (recommended): |
| 30 | +VS Code provides an easier 3-pane interface for resolving conflicts: |
| 31 | + |
| 32 | +-> Incoming Change → code from main (left/top) |
| 33 | + |
| 34 | +-> Current Change → code from your branch (right/top) |
| 35 | + |
| 36 | +-> Result → the lower/third pane, where you create the final merged file |
| 37 | + |
| 38 | +#### Steps to resolve: |
| 39 | + |
| 40 | +1. Open the conflicted file in VS Code. |
| 41 | + |
| 42 | +2. Look at both the Incoming Change and Current Change panels. |
| 43 | + |
| 44 | +3. In the Result (lower pane), edit the file so it contains the correct final version. |
| 45 | + |
| 46 | +-> Sometimes keep Incoming (main) |
| 47 | + |
| 48 | +-> Sometimes keep Current (your branch) |
| 49 | + |
| 50 | +-> Often, combine both parts. |
| 51 | + |
| 52 | +4. Save the file. If the conflict is resolved correctly, VS Code will mark it as fixed. |
| 53 | + |
| 54 | +5. If there are more conflicts, VS Code will automatically move you to the next one. Repeat until no conflicts remain. |
| 55 | + |
| 56 | +⚠️ Do NOT just click “Accept All Incoming” or “Accept All Current” — that usually deletes important code. |
| 57 | + |
| 58 | +## After fixing conflicts: |
| 59 | + |
| 60 | +### 4. Stage the resolved files |
| 61 | +```bash |
| 62 | +git add . |
| 63 | +``` |
| 64 | + |
| 65 | +### 5.Continue the rebase |
| 66 | +```bash |
| 67 | +git rebase --continue |
| 68 | +``` |
| 69 | +Repeat until all conflicts are resolved. |
| 70 | + |
| 71 | +### 6. After Rebase is Completed |
| 72 | + |
| 73 | +Once the rebase finishes successfully: |
| 74 | + |
| 75 | +- Your commits may look “different” (new commit hashes) — this is expected, since rebase rewrites history. |
| 76 | +- If you already have an open Pull Request, you will need to update it with a **force push**, before pushing, double-check that your commits are both DCO signed and GPG verified: |
| 77 | +```bash |
| 78 | +git log --show-signature |
| 79 | +``` |
| 80 | +then: |
| 81 | +```bash |
| 82 | +git push --force |
| 83 | +``` |
| 84 | + |
| 85 | +**Tip**: To be safe, create a backup branch before force pushing: |
| 86 | +```bash |
| 87 | +git checkout -b mybranch-backup |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +## Common issues |
| 92 | +Message: “No changes – did you forget to use git add?” |
| 93 | +→ This means you resolved the conflicts but forgot to stage them. Run git add . and try again. |
| 94 | + |
| 95 | +Message: “Are you sure you want to continue with conflicts?” |
| 96 | +→ This means some conflicts are still unresolved or you did not save the files properly. |
| 97 | +Double-check your files in VS Code, make sure they are saved, and resolve any remaining conflict markers (<<<<<<<, =======, >>>>>>>). |
| 98 | + |
| 99 | + |
| 100 | +## If you need to stop |
| 101 | +```bash |
| 102 | +git rebase --abort |
| 103 | +``` |
| 104 | + |
| 105 | +**Tip**: At each conflict: resolve → save → stage → continue. Repeat until all conflicts are gone. |
| 106 | + |
| 107 | +# What NOT to do |
| 108 | +1. ❌ Do not run git merge main |
| 109 | +→ This creates messy merge commits. Always rebase instead. |
| 110 | + |
| 111 | +2. ❌ Do not merge into your local main |
| 112 | +→ Keep main as a clean mirror of upstream/main. |
| 113 | + |
| 114 | +3. ❌ Do not open PRs from your fork’s main |
| 115 | +→ Always create a feature branch for your changes. |
| 116 | + |
| 117 | +At each conflict instance, you'll have to repeat: fix the conflict, stage the files and continue rebasing. |
| 118 | + |
| 119 | +## Recovery Tips: |
| 120 | + |
| 121 | +- Undo the last rebase commit, but keep changes staged (while still in rebase): |
| 122 | +If you are in the middle of a rebase and realize the last step went wrong, you can undo it while keeping changes staged: |
| 123 | +```bash |
| 124 | +git reset --soft HEAD~i |
| 125 | +``` |
| 126 | + |
| 127 | +Note: The number after HEAD~ refers to how many commits you want to go back. |
| 128 | + |
| 129 | +For example: |
| 130 | +HEAD~1 → go back 1 commit |
| 131 | +HEAD~3 → go back 3 commits |
| 132 | +HEAD~5 → go back 5 commits |
| 133 | + |
| 134 | +### If you are completely stuck |
| 135 | +Sometimes a rebase can get too messy to fix conflict by conflict. In that case, it’s often easier to start fresh: |
| 136 | + |
| 137 | +1. Abort the rebase to stop where you are: |
| 138 | +```bash |
| 139 | +git rebase --abort |
| 140 | +``` |
| 141 | + |
| 142 | +2. Reset your branch to match upstream/main: |
| 143 | +``` bash |
| 144 | +git checkout main |
| 145 | +git reset --hard upstream/main |
| 146 | +git checkout mybranch |
| 147 | +git rebase upstream/main -S |
| 148 | +``` |
| 149 | + |
| 150 | +⚠️ Use git stash only if you really want to save some local changes that aren’t yet committed. In most cases, if the rebase is failing, it’s safer to abort or reset rather than reapplying a stash of broken work. |
| 151 | + |
| 152 | + |
| 153 | +- To check commit signatures (verified status): |
| 154 | +```bash |
| 155 | +git log --pretty="%h %G? %aN %s" |
| 156 | +``` |
| 157 | +-> G = good signature (verified) |
| 158 | +-> B = bad signature |
| 159 | +-> U = unsigned commit |
| 160 | +-> N = no signature |
| 161 | + |
| 162 | +- To check for "Signed-off-by" lines: |
| 163 | +This shows commits along with their sign-off lines, helping you verify compliance with DCO (Developer Certificate of Origin). |
| 164 | +```bash |
| 165 | +git log --pretty="%h %s%n%b" | grep -B1 -A0 "Signed-off-by:" |
| 166 | +``` |
0 commit comments