【git】尝试在git中引发冲突并解决
处理可能是 Git 中最早遇到的挑战之一——解决冲突时的方法。
准备存储库。存储库的内容如下:
.
├── README.md
└── conflict.txt
为了制造冲突,将其克隆到两个本地位置。
https://github.com/kiyo27/git-practice
> git clone git@github.com:kiyo27/git-practice.git repo-a
> git clone git@github.com:kiyo27/git-practice.git repo-b
编辑repo-a的文件并推送到远程。
> cd repo-a
> echo "hello world v1." > conflict.txt
将变更转为远程更改。
> git commit -am "hello world v1"
> git push origin main
在repo-b中编辑文件。
> cd repo-b
> echo "hello world v2 > conflict.txt
提交更改
> git -am "hello world v2
试着远程推送
> git push origin main
> git push origin main
To github.com:kiyo27/git-practice.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'git@github.com:kiyo27/git-practice.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
当进行 git pull 操作时,会导致冲突的发生。
> git pull origin main
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 6 (delta 1), pack-reused 0
Unpacking objects: 100% (6/6), 718 bytes | 12.00 KiB/s, done.
From github.com:kiyo27/git-practice
* branch main -> FETCH_HEAD
bd537dc..27be7d3 main -> origin/main
Auto-merging conflict.txt
CONFLICT (content): Merge conflict in conflict.txt
Automatic merge failed; fix conflicts and then commit the result.
冲突.txt仍未合并。
> git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: conflict.txt
no changes added to commit (use "git add" and/or "git commit -a")
冲突文件的内容
> cat conflict.txt
<<<<<<< HEAD
hello world v2
=======
hello world v1.
>>>>>>> 940a9ee69fb3fe10f55c32d4beb01c5a604f2bc4
首先我们确认一下HEAD指的是什么。先检查一下历史记录。
// repo-bで作業
> git log --oneline
8aee435 (HEAD -> main) hello world v2
HEAD指向主分支的开头。
确认一下940a9ee69fb3fe10f55c32d4beb01c5a604f2bc4所代表的是什么。
> git log --oneline --no-abbrev-commit --all
8aee435f11c3b1e3840162816fb4702cbf6c1fa6 (HEAD -> main) hello world v2
940a9ee69fb3fe10f55c32d4beb01c5a604f2bc4 (origin/main, origin/HEAD) hello world v1.
940a9ee69fb3fe10f55c32d4beb01c5a604f2bc4指的是远程的main分支(repe-a的更改)。
在repo-b中应用更改。
<<<<<<< HEAD ← 消す
hello world v2
======= ← 消す
hello world v1. ← 消す
>>>>>>> 940a9ee69fb3fe10f55c32d4beb01c5a604f2bc4 ← 消す
修订之后
hello world v2
提交
> git add conflict.txt
> git commit
> git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
> git log --oneline
c43cfe1 (HEAD -> main) Merge branch 'main' of github.com:kiyo27/git-practice into main
8aee435 hello world v2
940a9ee (origin/main, origin/HEAD) hello world v1.
冲突问题已解决,将其远程推送。
> git push origin main