mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-21 02:10:11 +08:00
Merge pull request #29462 from wxy/20230404.1-⭐️-How-to-resolve-Git-merge-conflicts
ATRP:published/20230404.1 ⭐️ How to resolve Git merge conflicts.md
This commit is contained in:
commit
2d5aa58ea2
104
published/20230404.1 ⭐️ How to resolve Git merge conflicts.md
Normal file
104
published/20230404.1 ⭐️ How to resolve Git merge conflicts.md
Normal file
@ -0,0 +1,104 @@
|
||||
[#]: 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: "ChatGPT"
|
||||
[#]: reviewer: "wxy"
|
||||
[#]: publisher: "wxy"
|
||||
[#]: url: "https://linux.cn/article-15858-1.html"
|
||||
|
||||
如何解决 Git 合并冲突
|
||||
======
|
||||
|
||||
![][0]
|
||||
|
||||
> 在遇到合并冲突时,请不要惊慌。通过一些娴熟的技巧协商,你可以解决任何冲突。
|
||||
|
||||
假设你和我正在共同编辑同一个名称为 `index.html` 的文件。我对文件进行了修改,进行了提交,并将更改推送到 Git 远程仓库。你也对同一个文件进行了修改,进行了提交,并开始将更改推送到同一个 Git 仓库。然而,Git 检测到一个冲突,因为你所做的更改与我所做的更改冲突。
|
||||
|
||||
以下是你可以解决冲突的方法:
|
||||
|
||||
1、从远程仓库获取并合并最新更改:
|
||||
|
||||
```
|
||||
$ git pull
|
||||
```
|
||||
|
||||
2、识别一个或多个有冲突的文件:
|
||||
|
||||
```
|
||||
$ git status
|
||||
```
|
||||
|
||||
3、使用文本编辑器打开冲突文件:
|
||||
|
||||
```
|
||||
$ vim index.html
|
||||
```
|
||||
|
||||
4、解决冲突。冲突的修改会被标记为 `<<<<<<< HEAD` 和 `>>>>>>>`。你需要选择要保留和放弃哪些修改,并手动编辑文件以合并冲突的修改。
|
||||
|
||||
以下是一个示例:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
<div class="header">
|
||||
<h1>Sample text 1</h1>
|
||||
</div>
|
||||
=======
|
||||
<div class="header">
|
||||
<h1>Sample text 2</h1>
|
||||
</div>
|
||||
>>>>>>> feature-branch
|
||||
```
|
||||
|
||||
在这个例子中,我将网站标题更改为 `Sample text 1`,而你将标题更改为 `Sample text 2`。两种更改都已添加到文件中。现在你可以决定保留哪一个标题,或者编辑文件以合并更改。在任一情况下,删除指示更改开始和结束的标记,只留下你想要的代码:
|
||||
|
||||
```
|
||||
<div class="header">
|
||||
<h1>Sample text 2</h1>
|
||||
</div>
|
||||
```
|
||||
|
||||
5、保存所有更改,并关闭编辑器。
|
||||
|
||||
6、将文件添加到暂存区:
|
||||
|
||||
```
|
||||
$ git add index.html
|
||||
```
|
||||
|
||||
7、提交更改:
|
||||
|
||||
```
|
||||
$ git commit -m "Updated h1 in index.html"
|
||||
```
|
||||
|
||||
此命令使用消息 `Resolved merge conflict` 提交更改。
|
||||
|
||||
8、将更改推送到远程仓库:
|
||||
|
||||
```
|
||||
$ git push
|
||||
```
|
||||
|
||||
### 结论
|
||||
|
||||
合并冲突是将注意力集中于代码的好理由。你在文件中进行的更改越多,就越容易产生冲突。你应该进行更多的提交,每个提交更改应该更少。你应该避免进行包含多个特性增强或错误修复的单片巨大更改。你的项目经理也会感谢你,因为具有清晰意图的提交更容易追踪。在最初遇到 Git 合并冲突时可能会感到吓人,但是现在你知道如何解决它,你会发现它很容易解决。
|
||||
|
||||
*(题图:MJ/f432c41a-eb4f-4de0-b2da-f3d8d7a86e26)*
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/23/4/resolve-git-merge-conflicts
|
||||
|
||||
作者:[Agil Antony][a]
|
||||
选题:[lkxed][b]
|
||||
译者:ChatGPT
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/agantony
|
||||
[b]: https://github.com/lkxed/
|
||||
[0]: https://img.linux.net.cn/data/attachment/album/202305/30/090224e8pmumpfrmppghjr.jpg
|
@ -1,52 +0,0 @@
|
||||
[#]: 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