Welcome! Please hold on...

0 %
Kashif Sohail
Sr. Frontend Developer
  • Residence:
    Pakistan
  • City:
    Karachi
  • Age:
    26
Magento 1x 2x
Wordpress
Laravel
Angular
React
English
Urdu
  • Bootstrap, Tailwind
  • Sass, Less, Css
  • Webpack
  • GIT knowledge
0

No products in the cart.

Beginner’s guide to GitHub: Merging a pull request

September 17, 2024

[ad_1]

Welcome to GitHub for beginnersa series to help you navigate GitHub.

So far in this series we have covered the most important 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 repositoryAnd How to create your first pull request. However, sometimes things can get a little more complicated when changes conflict with each other. As you’ll see, GitHub has tools to help you navigate this potentially confusing situation.

Let’s get started!

Pull Request Summary

First, let’s review what a pull request (often referred to as PR) is and how it works. A Pull request is a proposal to move a set of changes from one branch to another. After you submit 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.

Once you’ve completed the review process, you can merge the changes into the target branch on GitHub by clicking the green “Commit Merge” button. This will merge the changes into the target branch and make the updates you suggested.

A review of a GitHub pull request shows comments about two new variables, remainderTime and startTime, added in script.js. The reviewer suggests using const where possible. The author agrees, but notes that this is beyond the scope of the current PR and will address it later. The conversation is marked as resolved.

What are merge conflicts?

Sometimes GitHub cannot automatically merge the changes into the target branch. This usually happens because of a Merger conflict.

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

Creating a merge conflict

To show you how to resolve a merge conflict, it is helpful to create one and walk through the steps required to resolve it. For this example, we will work in this repositoryWe have already submitted a pull request to this repository under the Update Name to update the Home file with a change to the header text. What we will do is create a new branch for this repository and commit another change in the same section of the Home File.

Open your terminal and navigate to the project directory for this repository. If you need help with these steps, see the previous entries in the GitHub Guide for Beginners. Once you are in your project directory, run git checkout -b add-new-name to create a new branch with the name add new nameIn the new branch, open the Home file and change the header text to have a different title than the existing pull request. Save the file and exit the editor.

After the changes have been made, you must apply 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 and drag the changes into the primarily branch. Don’t forget to complete the merge by clicking the green “Confirm Merge” button on the pull request page. Now these changes have been merged into the **main branch** of the repository.

Go back to the original pull request from Update Name Branch. If you scroll to the bottom of the page, you will see that you no longer have the option to save the changes in the primarily branch. Instead, you will see a message saying that there is a merge conflict that needs to be resolved.

A GitHub pull request displays a message indicating that the branch has conflicts that need to be resolved. The conflicting file is listed as index.html. The Merge Pull Request button is disabled and there is an option to resolve conflicts.

Resolve the merge conflict

There are several ways to resolve merge conflicts. One way is to click the “Resolve Conflicts” button on the pull request page. You can also resolve the merge conflicts locally by downloading the latest changes in the target branch. We will go through the second option.

Run in your terminal git switch main to primarily Branch. In this branch, you pull down the latest changes by clicking the git pull origin main command. Now navigate back to your Update Name Branching by executing git switch update-name in the terminal.

Run git merge main to try to implement the changes of Update Name in primarily. You will receive a message in the terminal that the automatic merge failed and that you need to resolve the conflicts before you can proceed with the merge. To correct the changes, open Home in your favorite code editor.

Depending on your code editor, the problematic section may be highlighted. If you are using a code editor with built-in GitHub support, such as VS Codesome options will appear in the highlighted section above.

A Visual Studio Code window displays 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 much fun!) and the pending change (Can you catch the moving cat?). Options are presented to accept or resolve the changes in the merge editor.

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

If your code editor does not have built-in GitHub support, you will need to manually edit the file to remove the conflicting sections. They are easily identified by multiple angle brackets (ie <<<<<<< at the beginning of the conflict and >>>>>>> at the end of the conflict).

When you're done editing the file, save your changes and exit the code editor. In the terminal, run the following commands to commit your changes and verify that all changes have been committed:

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

Finally, it is time to upload these changes to GitHub. You do this by git push origin update-name command. This will upload the changes that resolved the merge conflicts to your pull request. If you navigate back to the pull request on GitHub, you will see that the conflicts no longer exist and that you can see the changes in the primarily Branch by clicking the green "Merge Pull Request" button. Don't forget to click the "Confirm Merge" button at the end.

Congratulations! You have resolved the conflict and the primarily Branch with the desired changes!

Your next steps

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

Merging pull requests is an important skill to be able to competently use GitHub as a collaborative development tool, but with practice, resolving conflicts will feel as natural as pushing changes to GitHub.

If you have any questions, please ask them in the GitHub Community Thread and we will surely answer.

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

Written by

Kedasha Kerr

Kedasha is a Developer Advocate at GitHub, where she enjoys sharing her experiences with the wider developer community. She enjoys helping others learn more about the technology industry and loves sharing her experiences as a software developer. You can find her online at @itsthatladydev.

[ad_2]

Source link

Posted in TechnologyTags:
Write a comment