5

How do you resolve a merge conflict in Git

 1 year ago
source link: https://www.frontendinterviewquestions.com/interview-questions/how-do-you-resolve-a-merge-conflict-in-git
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do you resolve a merge conflict in Git

To resolve a merge conflict in Git, you can follow these steps:

1. Checkout the branch: Start by checking out the branch where the merge conflict occurred. Typically, this is the branch you were merging into, such as your feature branch or the main branch.


   git checkout <branch-name>
  

2. Pull the latest changes: It's essential to have the latest changes from the remote repository before resolving the conflict. Use the following command to pull the changes:


   git pull origin <branch-name>
 

3. Identify the conflicted files: Git will indicate the conflicted files in your project. You can use the `git status` command to see which files have conflicts. The conflicted files will have markers indicating the conflict within the file. For example:


   $ git status
   ...
   Unmerged paths:
     (use "git add <file>..." to mark resolution)
           both modified:   file.txt
 

4. Open the conflicted file: Open the conflicted file(s) in a text editor and locate the conflict markers. The conflict markers look something like this:


   <<<<<<< HEAD
   This is the content on your branch.
   =======
   This is the content from the branch you are merging.
   >>>>>>> branch-name

The content between `<<<<<<< HEAD` and `=======` is from your branch, and the content between `=======` and `>>>>>>> branch-name` is from the branch you are merging.

5. Resolve the conflict: Edit the conflicted file to resolve the conflict. Decide which changes to keep, modify the content as needed, and remove the conflict markers. For example, you might choose to keep one version of the code or combine the changes manually. After making the necessary changes, save the file.

6. Stage the resolved file: Once you have resolved the conflict in a file, you need to stage it to mark it as resolved. Use the following command for each resolved file:


   git add <file>
 

7. Commit the changes: After staging all the resolved files, commit the changes with a commit message describing the conflict resolution.


   git commit -m "Resolve merge conflict"

8. Push the changes: Finally, push the changes to the remote repository to complete the merge.


   git push origin <branch-name>

After following these steps, the merge conflict should be resolved, and your branch should be up to date with the changes from the other branch.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK