沖突產生的原因及解決辦法---push


場景一:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM