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
2. Identify one or more conflicting files :
$ git status
3. Use a text editor to open the conflict file:
$ vim index.html
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
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>
5. Save all changes, and close the editor.
6. Add files to the staging area:
$ git add index.html
7. Submit changes:
$ git commit -m "Updated h1 in index.html"
This command uses the message Resolved merge conflict
Submit changes .
8. Push changes to the remote repository:
$ git push
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!