Problem Description#
The issue began when I deployed lobechat on Vercel, but my forked repository frequently failed to sync with the original repository. Then I saw a request on the Pull requests page.
In the image, you can see there is a merge conflict that needs to be resolved in order to merge successfully.
On GitHub, a Pull Request (PR) is a request, usually initiated by a developer, to the maintainers of a project to merge changes from their branch into the main branch of the project. Pull Requests allow developers to discuss, review, and test code before merging, ensuring code quality and functionality meet expectations. (You can view details by clicking on the title of the Pull Request, including submitted changes, discussions, and review status.)
Why can my forked repository also receive requests from developers to merge into the original repository?
When you fork a repository, you create a completely independent copy of the original repository. However, if you have enabled GitHub Actions or certain automation tools in your forked repository, these tools may automatically create Pull Requests to sync updates from the original repository. This is usually to keep your fork in sync with the upstream repository's code.
Specific Situation#
After forking a repository (lobehub/lobe-chat
), I performed synchronization operations with the original repository to keep my forked repository consistent with it (i.e., pulling the latest changes from the original repository into my own branch). During the command execution, Git reported that some files had code conflicts, with the specific error as follows:
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Auto-merging README.zh-CN.md
CONFLICT (content): Merge conflict in README.zh-CN.md
Automatic merge failed; fix conflicts and then commit the result.
This is because my forked repository (origin
) and the original repository (upstream
) have modifications in certain files (such as README.md
and README.zh-CN.md
), and Git cannot automatically merge these conflicts, so they need to be resolved manually.
Problem-Solving Approach and Methods#
Based on the above issues, the solution can be divided into the following steps:
1. Configure the upstream
Remote Repository#
By default, after forking, it is only associated with your own GitHub repository (origin
), and the association with the original repository (upstream
) is not set. To sync with the original repository, you need to add a remote repository named upstream
.
git remote add upstream https://github.com/lobehub/lobe-chat.git
Explanation:
git remote add
: Used to add a remote repository.upstream
: This is the name of the remote repository (it is customary to name the original repositoryupstream
).https://github.com/lobehub/lobe-chat.git
: The address of the original repository.
You can confirm whether the remote repository is configured correctly with the following command:
git remote -v
Example output:
origin https://github.com/<your-username>/lobe-chat.git (fetch)
origin https://github.com/<your-username>/lobe-chat.git (push)
upstream https://github.com/lobehub/lobe-chat.git (fetch)
upstream https://github.com/lobehub/lobe-chat.git (push)
2. Pull the Latest Code from the Original Repository#
After configuring upstream
, you can pull the latest code from the original repository to your local machine:
git fetch upstream
This will fetch all updates from the upstream
repository and create an updated reference locally, such as upstream/main
.
Next, you need to merge the latest changes from upstream/main
into your own branch (assuming it is the main
branch):
git checkout main # Ensure you switch to the main branch
git merge upstream/main # Merge updates from the original repository into the current branch
If everything goes smoothly, Git will complete the automatic merge, but since we encountered conflicts earlier, Git will prompt the following error and stop:
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Auto-merging README.zh-CN.md
CONFLICT (content): Merge conflict in README.zh-CN.md
Automatic merge failed; fix conflicts and then commit the result.
3. Resolve Merge Conflicts#
At this point, you need to manually resolve the conflicts in the conflicting files.
3.1 Locate Conflict Files#
Run the following command to see which files have conflicts:
git status
The output will mark the conflicting files as both modified
, for example:
both modified: README.md
both modified: README.zh-CN.md
3.2 Edit Files to Resolve Conflicts#
Open these conflicting files using an editor (like VS Code). Git will add special markers in the conflict sections, formatted as follows:
<<<<<<< HEAD
(Content from branch HEAD)
=======
(Content from upstream/main)
>>>>>>> upstream/main
You need to edit as necessary, choosing which part to keep or merging both parts. For example:
Current File Content (Before Conflict)
<<<<<<< HEAD
My modified README content is here.
=======
A new update comes from the upstream repository.
>>>>>>> upstream/main
After Conflict Resolution (Adjusted)
Assuming you want to keep both parts and merge them:
My modified README content is here.
A new update comes from the upstream repository.
After editing, save the file and ensure that the conflict markers (<<<<<<<
, =======
, >>>>>>>
) have all been removed.
3.3 Mark Conflict Resolution Complete#
After resolving the conflicts, add the modified files to the staging area:
git add README.md README.zh-CN.md
Or use a wildcard to add all files at once:
git add .
3.4 Commit the Merge Result#
Once all conflicts are resolved and added to the staging area, commit the resolution of the merge:
git commit -m "Resolve merge conflicts in README files"
4. Push Local Changes to the Remote Repository#
After completing the merge, push your local changes to your forked repository:
git push origin main
At this point, GitHub will show that your branch is now in sync with the original repository. If this was to resolve a Pull Request conflict on GitHub, you can now complete the PR merge on the GitHub website.
Summary#
This is a complete practice of handling Git merge conflicts. Through the above operations, I successfully resolved the file conflict issues and completed the synchronization of updates from the original repository to my forked repository.
- Problem: During the synchronization of updates from the original repository, modifications to the same file by two branches led to merge conflicts.
- Solution:
- Configure and pull updates from the original repository.
- Identify conflicting files when merging updates into the current branch.
- Manually resolve the marked content in the conflicting files, keeping the appropriate code.
- Commit the modifications and push updates to the remote repository.
Summary of Related Key Commands:
- Add
upstream
remote repository:git remote add upstream <url>
- Pull and sync with the original repository:
git fetch upstream git merge upstream/main
- View conflicting files:
git status
- After automatically or manually resolving conflicts, commit changes:
git add <file> git commit -m "Resolve conflicts" git push origin main
Through this practice, I gained a deeper understanding of the process of resolving Git merge conflicts. Whether syncing with the original repository or handling complex team collaboration scenarios, these skills are very important. I hope this can help others with similar issues!