场景一:
1. 远端仓库有一个文件test1.py
def test(): print("haha")
2. 同事1,同事一,将这个文件
同事1,将远端的代码修改后
(base) yanyandeMBP:p1 yanyanzhang$ ls test1.py (base) yanyandeMBP:p1 yanyanzhang$ vim test1.py (base) yanyandeMBP:p1 yanyanzhang$ git add test1.py (base) yanyandeMBP:p1 yanyanzhang$ git status On branch master Your branch is up to date with ‘origin/master‘. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: test1.py (base) yanyandeMBP:p1 yanyanzhang$ git commit -m "add p1" [master 03cf73f] add p1 1 file changed, 1 insertion(+) (base) yanyandeMBP:p1 yanyanzhang$ git push Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-5.0] To gitee.com:meloncodezhang/git_study.git 47fff5f..03cf73f master -> master (base) yanyandeMBP:p1 yanyanzhang$ git branch -l * master (base) yanyandeMBP:p1 yanyanzhang$
远端代码变为,此时远端代码是最新的。
def test(): print("add by p1") print("haha")
同事2,并不知道远端的代码已经改变了,也没有pull,此时自己本地代码为,并不是最新的代码
def test(): print("haha")
同事2,将本地代码修改后,add commit push
def test(): print("add by p2") print("haha")
此时报错
(base) yanyandeMBP:p2 yanyanzhang$ ls test1.py (base) yanyandeMBP:p2 yanyanzhang$ vim test1.py (base) yanyandeMBP:p2 yanyanzhang$ git add test1.py (base) yanyandeMBP:p2 yanyanzhang$ git status On branch master Your branch is up to date with ‘origin/master‘. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: test1.py (base) yanyandeMBP:p2 yanyanzhang$ git commit -m "add p2" [master 7dd9ad0] add p2 1 file changed, 1 insertion(+) (base) yanyandeMBP:p2 yanyanzhang$ git push To gitee.com:meloncodezhang/git_study.git ! [rejected] master -> master (fetch first)
# 产生的本质原因是,push之前,会将远端仓库的代码和本地仓库的代码进行比较
# 1. 远端代码有的本地没有,就立即报错,远端代码有 print("add by p1"), 而本地commit到本地仓库,并没有一行代码,因此导致不能push
# 2. 远端的代码本地都有,而且本地还新增了代码,这时可以push成功,git会自动将远端代码和本地代码进行合并,将本地新增的内容同步到远端上去。 error: failed to push some refs to ‘git@gitee.com:meloncodezhang/git_study.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. (base) yanyandeMBP:p2 yanyanzhang$
解决办法一:手动解决冲突
直接pull远端代码,
(base) yanyandeMBP:p2 yanyanzhang$ git pull remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From gitee.com:meloncodezhang/git_study 47fff5f..03cf73f master -> origin/master Auto-merging test1.py CONFLICT (content): Merge conflict in test1.py Automatic merge failed; fix conflicts and then commit the result. # 将远端的代码和本地代码进行自动合并出错,原理也是一样,会将远端的代码和本地代码进行比较
# 比较也有两个结果
# 1. 远端仓库的代码有,本地仓库代码没有,会将本地代码更新成远端的样子。
# 2. 本地仓库有的代码有的,远端仓库代码没有,会发生自动合并失败,因此需要手动解决冲突后才能合并 (base) yanyandeMBP:p2 yanyanzhang$
手动解决, <<<<< HEAD 代码 >>>>> xxxxxx 之间包裹的代码,就是冲突产生的地方,====== 上面是本地仓库的代码 ====== 下面是远端仓库的代码,就是因为这两行不一样,因此才产生了冲突。我们选择选用我们自己的代码,删除 ==== 下面的代码。
1 2 def test(): 3 <<<<<<< HEAD 4 print("add by p2") 5 ======= 6 print("add by p1") 7 >>>>>>> 03cf73f7e01321623ab2a28fd86f15aee7e3d132 8 print("haha")
删除后变为
def test(): print("add by p2") print("haha")
然后执行 add commit push,解决冲突后将代码重新push后pull下来
(base) yanyandeMBP:p2 yanyanzhang$ cat test1.py def test(): print("add by p2") print("haha") (base) yanyandeMBP:p2 yanyanzhang$ git add test1.py (base) yanyandeMBP:p2 yanyanzhang$ git status On branch master
# 我的代码和远端代码已经有分歧了,有一个改变了 Your branch and ‘origin/master‘ have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) # 所有冲突解决,使用commit将我的代码合并到远端。 All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: test1.py (base) yanyandeMBP:p2 yanyanzhang$ git commit -m "p2 change" [master 6718c01] p2 change (base) yanyandeMBP:p2 yanyanzhang$ git status On branch master Your branch is ahead of ‘origin/master‘ by 2 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean (base) yanyandeMBP:p2 yanyanzhang$ git push Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (6/6), 549 bytes | 549.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-5.0] To gitee.com:meloncodezhang/git_study.git 03cf73f..6718c01 master -> master (base) yanyandeMBP:p2 yanyanzhang$ git pull Already up to date.
这种解决的办法就是,让修改后的代码变为远端最新代码。
看看远端仓库
def test(): print("add by p2") print("haha")
正确的Push顺序是, add commit pull(是否有冲突需要解决)push
原文:https://www.cnblogs.com/meloncodezhang/p/14772695.html