1. 程式人生 > >git多人合作如何有序的進行提交合並

git多人合作如何有序的進行提交合並

我們在實際的多人合作中,時常用到git,那麼怎麼樣才能有序的進行提交操作呢?

例子:直接以遠端端的origin/master為遠端開發分支(注:實際中常常在遠端分支,建立dev分支用於日常開發,dev不是很穩定,而master分支常用於存放穩定的一個版本,dev開發完畢後,才會合併到master)。

如果在開發過程中,你提交的內容,與遠端端沒有衝突,通過簡單的,三個步驟,即可完成提交:

git add .

git commit -m "test"
[master 26e919d] test
 1 file changed, 1 insertion(+), 1 deletion(-)

git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To *****
*************************.git c11b82b..26e919d master -> master

但是比如存在一個檔案例如readme.rst,在你提交之前,其他人已經對此檔案做了修改,再按照上述步驟,執行到git push時,就會出現錯誤,例如:

git add .

git commit -m "test"
[master 060b314] test
 1 file changed, 1 insertion(+), 1 deletion(-)

git push origin master
To ******************************.git
 ! [rejected]        master -> master (fetch first
) error: failed to push some refs to '******************************.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.

此時我們要做的是,獲取遠端端的最新狀態,並在本地處理衝突,然後再執行push操作

用git pull 獲取遠端端最新版本

git pull origin master

然後用git status 檢視的狀態(可檢視衝突檔案)

git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   readme.rst

no changes added to commit (use "git add" and/or "git commit -a")

然後我們到readme.rst中修改衝突的地方

vi readme.rst

<<<<<<< HEAD
123456
=======
12345abc
>>>>>>> f01e511d9298789e7d1c55c4803b2120fb23f7c5

衝突部分會被

<<<<<<< HEAD

=======

>>>>>>>

包裹,上面的部分為本地的,下面的部分為遠端端的,只需要根據實際情況,修改或刪除相應的內容就可以了,然後在重新提交。

12345abc

git add readme.rst

git commit -m "conflict fixed"

git push origin master

就能成功提交了。