解决冲突
若要继续推送我们刚做的修改到远程存储库,我们就必须手动解决冲突。为此,让我们执行拉取以从远程存储库获取最新的更改集。
执行以下 git pull 命令:
$ git pull origin main
Username:
Password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://example.backlog.com/git/BLGGIT/tutorial.git
* branch main -> FETCH_HEAD
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
Automatic merge failed; fix conflicts and then commit the result.
将出现一条消息,警告我们存在合并冲突。
当您打开sample.txt
时,您会看到 Git 添加的标记表明该文件章节存在冲突,如下所示。
Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of the remote repository
>>>>>>> 17c860612953c0f9d88f313c8dfbf7d858e02e91
我们将通过接受两个更改,并移除标记来解决冲突。
Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of the remote repository
一旦我们解决了冲突,并且文件的内容发生变化后,我们将提交更改。
$ git add sample.txt
$ git commit -m "merge"
[main d845b81] merge
我们现在更新了来自远程存储库的最新更改。
我们可以使用 git log 命令来验证存储库历史记录的准确性。--graph
选项将以图形格式显示分支历史记录,--online
选项将尝试压缩输出消息。
$ git log --graph --oneline
* d845b81 merge
|\
| * 4c01823 append description of the pull command
* | 95f15c9 append description of the commit command
|/
* 3da09c1 append description of the add command
* ac56e47 first commit
这表明这两个历史记录已成功与新的合并提交合并了。 我们现在可以安全地将此更改推送到远程存储库,而不会发生任何合并冲突。