pull时的冲突问题
场景一:
day1
远程仓库文件版本是1
昨天早上,clone了远程仓库到A电脑,创建edit分支并开始编辑,本地仓库文件版本1
昨天晚上,编辑后的文件版本为12,忘记commit和push了
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md
# git_learning
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ echo 12 >> README.md
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md
# git_learning
12
day2
今天早上,clone了远程仓库到B电脑,创建edit分支并开始编辑,本地仓库版本1
今天晚上,编辑后的文件版本3,本地add,commit,merge,push,那么远程仓库文件版本13
jw@PC MINGW64 /f/桌面文件
$ cd git_learning/
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ git checkout -b edit
Switched to a new branch 'edit'
jw@PC MINGW64 /f/桌面文件/git_learning (edit)
$ cat README.md
# git_learning
jw@PC MINGW64 /f/桌面文件/git_learning (edit)
$ echo >> 13 README.md
jw@PC MINGW64 /f/桌面文件/git_learning (edit)
$ git add .
warning: LF will be replaced by CRLF in 13.
The file will have its original line endings in your working directory
jw@PC MINGW64 /f/桌面文件/git_learning (edit)
$ git commit . -m "13"
warning: LF will be replaced by CRLF in 13.
The file will have its original line endings in your working directory
[edit 3ad57cf] 13
1 file changed, 1 insertion(+)
create mode 100644 13
jw@PC MINGW64 /f/桌面文件/git_learning (edit)
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ git merge edit
Updating 64dccd9..3ad57cf
Fast-forward
13 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 13
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ git push -u origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 264 bytes | 52.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Jan0510/git_learning.git
64dccd9..3ad57cf main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
day3
明天早上,在A电脑上第一件事是pull,但是发生冲突。因为远程仓库版本为13,本地仓库版本为12
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md
# git_learning
12
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), 253 bytes | 14.00 KiB/s, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
From github.com:Jan0510/git_learning
3ad57cf..2a0c560 main -> origin/main
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting
Updating 3ad57cf..2a0c560
意思就是:本地修改还没有保存,pull操作会将远程仓库的版本覆盖掉本地修改。
解决办法:commit(提交)或者stash(暂存)本地修改,再pull
stash
stash的基本用法步骤如下
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash # 暂存本地修改
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
Saved working directory and index state WIP on main: 3ad57cf 13
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md # 可以看到12没有了
# git_learning
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash list # 可以看到保存的信息,其中stash@{0}就是刚才保存的标记
stash@{0}: WIP on main: 3ad57cf 13
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash pop stash@{0} # 还原暂存的内容
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (1682ebeaf6836d534584351e8a9f905be754d29f)
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md # 可以看到12又回来了
# git_learning
12
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash drop # 默认丢弃最上面那条
解决方案:
- 希望保留12和13,stash暂存本地修改,再pull,再还原暂存内容,再解决冲突文件
- 不需要保留12,stash暂存本地修改,再drop丢弃暂存的内容,再pull
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash
Saved working directory and index state WIP on main: 3ad57cf 13
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md
# git_learning
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git pull
Updating 3ad57cf..2a0c560
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git stash pop stash@{0}
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
The stash entry is kept in case you need it again.
在还原暂存内容时,提示冲突文件,直接打开冲突文件:
# git_learning
<<<<<<< Updated upstream
13
=======
12
>>>>>>> Stashed changes
<<<<<<< Updated upstream和=======中间的是pull后的内容, ======= 和>>>>>>> Stashed changes中间的是stash pop出来的内容
自己确定保留那一部分代码,最后删除<<<<<<< HEAD ,======= ,>>>>>>>这种标志
那么,我将该文件修改成:
# git_learning
13
12
保存,add,commit,push
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git add .
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git commit . -m "1312"
[main b4b65db] 1312
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 13
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 251 bytes | 83.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Jan0510/git_learning.git
2a0c560..b4b65db main -> main
commit
解决方案:add,commit,再pull
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git pull
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting
Updating 64dccd9..b4b65db
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git commit . -m "12"
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
[main 07b5701] 12
1 file changed, 1 insertion(+)
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ git pull
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
commit,pull后又提示冲突,直接打开冲突文件,可以看到
# git_learning
<<<<<<< HEAD
=======
13
>>>>>>> b4b65dbd3ae898598ee09c8a06fdea992856ea48
12
将标记清除,并保留最终版本
# git_learning
13
12
然后add,commit,push更新远程仓库
jw@PC MINGW64 ~/Desktop/git_learning (main|MERGING)
$ git add .
jw@PC MINGW64 ~/Desktop/git_learning (main|MERGING)
$ git commit -m "1312" # 没有指定文件,只为提交merge
[main da37281] 1312
jw@PC MINGW64 ~/Desktop/git_learning (main)
$ cat README.md
# git_learning
13
12
场景二:
在本地直接使用init创建仓库,然后自顾自的创建了许多文件。
由于一开始没有从远程仓库clone下来,所以导致本地初始仓库的内容与远程仓库的内容不一致。
假如我之前是直接clone的方式在本地建立起远程github仓库的克隆本地仓库就不会有这问题了。
内容不一致,想要把远程仓库的内容pull下来合并时:
$ git remote add origin git@github.com:Jan0510/threading_learning.git
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> main
$ git pull origin main
From github.com:Jan0510/threading_learning
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
$ git branch --set-upstream-to=origin/main main
$ git pull
fatal: refusing to merge unrelated histories
解决办法:pull命令后紧接着使用--allow-unrelated-histories
选项来解决问题(该选项可以合并两个独立启动仓库的历史)。
jw@PC MINGW64 ~/Desktop/threading_learning (main)
$ git pull --allow-unrelated-histories
CONFLICT (add/add): Merge conflict in SerialThread.py
Auto-merging SerialThread.py
Automatic merge failed; fix conflicts and then commit the result.
2个不相干的仓库合并,会出现冲突,打开冲突文件,编辑冲突内容后,执行add,commit,push
jw@PC MINGW64 ~/Desktop/threading_learning (main|MERGING)
$ git add .
jw@PC MINGW64 ~/Desktop/threading_learning (main|MERGING)
$ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: SerialThread.py
new file: main.py
jw@PC MINGW64 ~/Desktop/threading_learning (main|MERGING)
$ git commit -m "1"
[main 525157c] 1
jw@PC MINGW64 ~/Desktop/threading_learning (main)
$ git branch --set-upstream-to=origin/main main
$ git push
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 4 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (15/15), 15.51 KiB | 1.55 MiB/s, done.
Total 15 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), completed with 1 local object.
To github.com:Jan0510/threading_learning.git
36f2742..525157c main -> main
push
场景一:
早上第一件事就是:pull,然后建立edit分支,然后开始编辑
到了晚上,准备push,发现冲突。
原因:其他人在下午的时候,push更新了远程仓库。导致本地版本不跟上远程版本。
解决:我在push前,先pull到main,再解决出现的冲突
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ git push
To github.com:Jan0510/git_learning.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:Jan0510/git_learning.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.
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 233 bytes | 1024 bytes/s, done.
From github.com:Jan0510/git_learning
da37281..0c7b271 main -> origin/main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
jw@PC MINGW64 /f/桌面文件/git_learning (main|MERGING)
$ cat README.md
# git_learning
13
12
<<<<<<< HEAD
15
=======
14
>>>>>>> 0c7b27149376369856cc587b7ff351cf27d759e9
直接打开冲突文件,解决冲突并且去除标记,文件修改成下面这样
# git_learning
13
12
15
14
接着是add,commit,push
jw@PC MINGW64 /f/桌面文件/git_learning (main|MERGING)
$ 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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
jw@PC MINGW64 /f/桌面文件/git_learning (main|MERGING)
$ git add .
jw@PC MINGW64 /f/桌面文件/git_learning (main|MERGING)
$ 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)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: README.md
jw@PC MINGW64 /f/桌面文件/git_learning (main|MERGING)
$ git commit -m "1415"
[main cf3b2ce] 1415
jw@PC MINGW64 /f/桌面文件/git_learning (main)
$ 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