How to resolve Git merge conflicts

WBOY
Release: 2023-06-09 15:58:56
forward
1980 people have browsed it

如何解决 Git 合并冲突

Suppose you and I are co-editing the same file named index.html. I made changes to the file, committed it, and pushed the changes to the Git remote repository. You also made changes to the same file, made a commit, and started pushing changes to the same Git repository. However, Git detected a conflict because the changes you made conflict with the changes I made.

Here's how you can resolve conflicts:

1. Get and merge the latest changes from the remote repository:

$ git pull
Copy after login

2. Identify one or more conflicting files :

$ git status
Copy after login

3. Use a text editor to open the conflict file:

$ vim index.html
Copy after login

4. Resolve the conflict. Conflicting modifications will be marked <<<<<<< HEAD and . You need to choose which changes to keep and discard, and manually edit the file to merge conflicting changes.

Here is an example:

<<<<<<< HEAD<div ><h1>Sample text 1</h1></div>=======<div ><h1>Sample text 2</h1></div>>>>>>>> feature-branch
Copy after login

In this example, I changed the website title to Sample text 1, and you changed the title to Sample text 2. Both changes have been added to the file. Now you can decide which header to keep, or edit the file to merge the changes. In either case, remove the markers indicating the beginning and end of the changes, leaving just the code you want:

<div ><h1>Sample text 2</h1></div>
Copy after login

5. Save all changes, and close the editor.

6. Add files to the staging area:

$ git add index.html
Copy after login

7. Submit changes:

$ git commit -m "Updated h1 in index.html"
Copy after login

This command uses the message Resolved merge conflict Submit changes .

8. Push changes to the remote repository:

$ git push
Copy after login

Conclusion

Merge conflicts are a good reason to focus on the code. The more changes you make in a file, the easier it is to create conflicts. You should make more commits and each commit should change less. You should avoid making monolithic huge changes that include multiple feature enhancements or bug fixes. Your project manager will thank you, too, because commits with clear intent are easier to track. It might be scary when you first encounter a Git merge conflict, but now that you know how to resolve it, you'll find that resolving it is easy.

The above is the detailed content of How to resolve Git merge conflicts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template