Resolve Merge Conflicts
Scenario
Ideally, each repository should have at least two Pull Requests (PRs) modifying the same gdsc.txt file with contact information. Merging one PR may cause a conflict in the other PR due to changes in the same file.
Steps to Solve Conflicts
The primary difference between the merge and rebase approaches lies in how they integrate changes.
Merge combines the commit histories of the divergent branches, creating a new commit that reflects the combined changes. It retains the original commit history but may result in "merge commits," which explicitly mark where the branches were merged.
Rebase, on the other hand, rewrites the commit history by placing the local branch's commits on top of the latest changes from the main branch. This creates a linear and cleaner history but alters commit timestamps.
Merge Approach:
-
Checkout the Branch:
- Navigate to the branch with the conflicting changes:
git checkout branch-name
- Navigate to the branch with the conflicting changes:
-
Pull the Latest Changes:
- Fetch the latest changes from the remote repository:
git pull origin main
- Fetch the latest changes from the remote repository:
-
Resolve Conflicts Locally:
- Open the
gdsc.txtfile in your code editor. - Resolve conflicts using the editor's conflict resolution tool or manually by editing the file.
- The conflict markers (
<<<<<<<,=======,>>>>>>>) denote conflicting sections. - Use the editor's tool to choose and retain the desired changes from both versions.
- Ensure the file reflects the intended information after conflict resolution.
- The conflict markers (
- Open the
-
Stage and Commit Changes:
- After resolving conflicts, stage the modified file:
git add gdsc.txt - Commit the changes:
git commit -m "Resolved merge conflict in gdsc.txt"
- After resolving conflicts, stage the modified file:
-
Push Changes to the Branch:
- Push the changes to the branch:
git push origin branch-name
- Push the changes to the branch:
-
Merge the PR:
- Proceed to merge the conflicted PR through the repository interface.
Rebase Approach:
-
Checkout the Branch:
- Navigate to the branch with the conflicting changes:
git checkout branch-name
- Navigate to the branch with the conflicting changes:
-
Rebase from Main:
- Rebase the branch with the latest changes from the main branch:
git rebase main
- Rebase the branch with the latest changes from the main branch:
-
Resolve Conflicts:
- Address any conflicts encountered during the rebase process by following the prompts in your terminal.
-
Continue Rebase:
- After conflicts are resolved, continue the rebase:
git rebase --continue
- After conflicts are resolved, continue the rebase:
-
Push the Rebased Changes:
- Push the rebased changes to the remote branch:
git push --force origin branch-name
What is --force?
The `--force` flag is used to force push the rebased changes to the remote branch. This is required because the rebase process rewrites the commit history of the branch. - Push the rebased changes to the remote branch:
-
Merge the PR:
- Proceed to merge the rebased PR through the repository interface.