Posted in

GitHub 入门指南:合并 Pull Request_AI阅读总结 — 包阅AI

包阅导读总结

1. `GitHub`、`Pull Request`、`Merge Conflicts`、`Resolving`、`Beginners Guide`

2. 本文是针对初学者的 GitHub 指南,主要介绍了合并拉取请求的相关内容,包括拉取请求的概念和作用,以及当出现合并冲突时如何解决,鼓励读者练习并提供了更多资源。

3.

– 开篇介绍系列旨在帮助初学者轻松使用 GitHub

– 回顾之前涵盖的内容

– 拉取请求回顾

– 解释拉取请求及工作原理

– 完成审查后可合并到目标分支

– 合并冲突

– 解释合并冲突的原因

– 制造合并冲突

– 创建新分支并修改同一文件的相同部分

– 推送更改并创建拉取请求,引发冲突

– 解决合并冲突

– 可点击页面“解决冲突”按钮或本地处理

– 以本地处理为例,通过终端操作,在代码编辑器中解决

– 提交更改并推送到 GitHub 以解决冲突

– 后续步骤

– 鼓励练习,提供更多资源

4.

思维导图:

文章地址:https://github.blog/developer-skills/github-education/beginners-guide-to-github-merging-a-pull-request/

文章来源:github.blog

作者:Kedasha Kerr

发布时间:2024/8/26 13:00

语言:英文

总字数:1101字

预计阅读时间:5分钟

评分:88分

标签:GitHub,Pull Request,合并冲突,版本控制,开发者技能


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

Welcome back to GitHub for Beginners, a series designed to help you navigate GitHub with ease.

So far in this series, we’ve covered the top Git commands every developer should know, how to create repositories, how to upload files and folders to your repository, how to add code to your repository, and how to create your first pull request. However, sometimes it can be a little bit more complicated when changes conflict with each other. As you will see, GitHub has tools to help you navigate this potentially confusing situation.

Let’s get started!

VIDEO

Pull request recap

First, let’s review what a pull request is (often referred to as a PR) and how it works. A pull request is a proposal to move a set of changes from one branch into another. After submitting a pull request, other team members can review your proposed changes before accepting them. They can review your code, test your changes locally, and provide feedback.

A GitHub pull request review shows comments on two new variables remainingTime and startTime added in script.js. The reviewer suggests using const where possible. The author agrees but notes it's beyond the current PR's scope and will address it later. The conversation is marked as resolved.

Once you have completed the review process, you can merge the changes into the target branch on GitHub by clicking the green button that says “Confirm merge.” This pulls the changes into the target branch, making your proposed updates.

A GitHub pull request review shows comments on two new variables remainingTime and startTime added in script.js. The reviewer suggests using const where possible. The author agrees but notes it's beyond the current PR's scope and will address it later. The conversation is marked as resolved.

What are merge conflicts?

Sometimes, GitHub can’t automatically merge the changes into the target branch. This usually happens because of a merge conflict.

Since the same section of the file changed, GitHub doesn’t know which version of the changes you want to keep, so in order to merge a pull request on GitHub, you must first resolve all merge conflicts.

Creating a merge conflict

To show you how to resolve a merge conflict, it’s helpful to create one and walk through the steps necessary to fix it. For this example, we are working in this repository. We already have a pull request in this repository under the update-name branch to update the index.html file with a change to the header text. What we’re going to do is create a new branch for this repository and introduce a different change to the same section of the index.html file.

Open your terminal and navigate to the project directory for this repository. If you need help with these steps, refer to the earlier entries in the GitHub for Beginners guide. Once you’re in your project directory, run git checkout -b add-new-name to create a new branch named add-new-name. In the new branch, open the index.html file and change the header text to be a different title from the existing pull request. Save the file and exit your editor.

Now that the changes have been made, you need to push them by running git push origin add-new-name in the terminal.

Navigate to the repository on GitHub and create a pull request for the add-new-name branch, pulling the changes into the main branch. Don’t forget to finish the merge by clicking the green “Confirm merge” button on the pull request page. Now, these changes have been integrated into the **main **branch of the repository.

Make your way back to the original pull request from the update-name branch. If you scroll to the bottom of the page, you will see that you no longer have the option to merge the changes into the main branch. Instead, you’ll see a message that there’s a merge conflict that needs to be resolved.

A GitHub pull request shows a message indicating that the branch has conflicts that must be resolved, with the conflicting file listed as index.html. The Merge pull request button is disabled, and there is an option to Resolve conflicts.

Resolving the merge conflict

There are a couple of ways that you can resolve merge conflicts. One option is to click the “Resolve conflicts” button on the pull request page. You can also address the merge conflicts locally by pulling down the latest changes in the target branch. We’re going to walk through the second option.

In your terminal, run git switch main to navigate to the main branch. In that branch, pull down the latest changes by running the git pull origin main command. Now, navigate back to your update-name branch by running git switch update-name in the terminal.

Run git merge main to attempt to merge the changes from update-name into main. You’ll receive a message in the terminal that the automatic merge failed and that you need to resolve the conflicts before you can continue merging. To fix the changes, open up index.html in your code editor of choice.

Depending on your code editor, the problematic section might be highlighted. If you’re using a code editor with integrated GitHub support, such as VS Code, you’ll see some options at the top of the highlighted section.

A Visual Studio Code window shows a merge conflict in the index.html file for the Catch the Cat Game project. The conflict is between the current change (Catch the Cat! It's so fun!) and the incoming change (Can you catch the moving cat?). Options to accept changes or resolve in the merge editor are displayed.

If so, click the option that says “Accept Current Change.” This will edit the file so that only the incoming change remains in the file.

If your code editor doesn’t have integrated GitHub support, you will need to manually edit the file to remove the conflicting sections. They can be easily identified by the presence of several angle brackets (that is, <<<<<<< at the start of the conflict and >>>>>>> at the end of the conflict).

Once you’ve finished editing the file, save your changes and quit the code editor. Back in the terminal, run the following commands to commit your changes and verify that all of the changes have been committed:

git add .git commit -m "resolve merge conflict"git status

Finally, it’s time to push these changes up to GitHub. You do this by running the git push origin update-name command. This uploads the changes that have resolved the merge conflicts into your pull request. If you navigate back to the pull request on GitHub, you’ll see that the conflicts no longer exist and that you can merge the changes into the main branch by clicking the green “Merge pull request” button. Last but not least, don’t forget to click the “Confirm merge” button.

Congratulations! You’ve resolved the conflict and updated the main branch with the requested changes!

Your next steps

If you want to practice creating pull requests and resolving merge conflicts, you can use this repository and follow the instructions in the README.md file.

Merging pull requests is an essential skill in order to be proficient with using GitHub as a collaborative development tool. However, with some practice, resolving conflicts will feel as natural as pushing changes to GitHub.

If you have any questions, pop them in the GitHub Community thread and we’ll be sure to respond.

Here are some more resources to help you on your GitHub journey:

Written by

Kedasha is a Developer Advocate at GitHub where she enjoys sharing the lessons she’s learned with the wider developer community. She finds joy in helping others learn about the tech industry and loves sharing her experience as a software developer. Find her online @itsthatladydev.