mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #29058 from lkxed/20230404-1-How-to-resolve-Git-merge-conflicts
[手动选题][tech]: 20230404.1 ⭐️ How to resolve Git merge conflicts.md
This commit is contained in:
commit
2931ff46b4
@ -0,0 +1,52 @@
|
||||
[#]: subject: "How to resolve Git merge conflicts"
|
||||
[#]: via: "https://opensource.com/article/23/4/resolve-git-merge-conflicts"
|
||||
[#]: author: "Agil Antony https://opensource.com/users/agantony"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
How to resolve Git merge conflicts
|
||||
======
|
||||
|
||||
Suppose you and I are working on the same file called **index.html**. I make some changes to the file, commit them, and push the changes to the remote Git repository. You also make some changes to the same file, commit them, and start pushing the changes to the same Git repository. However, Git detects a conflict because the changes you made conflict with the changes I made.
|
||||
|
||||
Here's how you can resolve the conflict:
|
||||
|
||||
- Fetch and merge the latest changes from the remote repository:`$ git pull`
|
||||
- Identify the one or more conflicting files:`$ git status`
|
||||
- Open the conflicting file using a text editor:`$ vim index.html`
|
||||
- Resolve the conflict. The conflicting changes are marked by `<<<<<<< HEAD` and `>>>>>>>`. You need to choose which changes to keep and which to discard. Manually edit the file to combine the conflicting changes. Here's an example:`<<<<<<< HEAD
|
||||
<div class="header">
|
||||
<h1>Sample text 1</h1>
|
||||
</div>
|
||||
=======
|
||||
<div class="header">
|
||||
<h1>Sample text 2</h1>
|
||||
</div>
|
||||
>>>>>>> feature-branch`In this example, I changed the website heading to `Sample text 1`, while you have changed the heading to `Sample text 2`. Both changes have been added to the file. You can now decide which heading to keep or edit the file to combine the changes. In either case, remove the markers that indicate the beginning and end of the changes, leaving only the code you want:`<div class="header">
|
||||
<h1>Sample text 2</h1>
|
||||
</div>`
|
||||
- Save all of your changes, and close your editor.
|
||||
- Add the file to the staging area:`$ git add index.html`
|
||||
- Commit the changes:`$ git commit -m "Updated h1 in index.html"`This command commits the changes with the message `Resolved merge conflict`.
|
||||
- Push the changes to the remote repository:`$ git push`
|
||||
|
||||
### Resolution
|
||||
|
||||
Merge conflicts are a good reason to focus your changes on code. The more you change in a file, the greater the potential for conflict. You should make more commits with fewer changes each. You should avoid making a monolithic change that combines multiple feature enhancements or bug fixes into one. Your project manager will thank you, too, because commits with clear intent are easier to track. A Git merge conflict may seem intimidating at first, but now that you know how to do it, you'll see that it's easily resolved.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/4/resolve-git-merge-conflicts
|
||||
|
||||
作者:[Agil Antony][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/agantony
|
||||
[b]: https://github.com/lkxed/
|
Loading…
Reference in New Issue
Block a user