git push :推送本地更改到遠程倉庫的三種模式

來源:互聯網
上載者:User

摘要:由於在git push過程中,no-fast-forward 的push會被拒絕,如何解決git push失敗的問題。這裡面有三種方法,分別會形成merge形式的提交曆史,線性形式的提交曆史,覆蓋原來的提交曆史。


本文來源:git push 的三種模式

地址:http://blog.csdn.net/trochiluses/article/details/14517379 1.git push產生衝突的形成過程


現在,伺服器端最新版本是x;使用者甲和使用者已分別clone代碼,然後進行開發;使用者甲完成版本A,並提交,此時伺服器端版本曆史變成:

X------A

使用者已機器上的版本提交曆史是X--------B

而git push的結果說明是這樣的: Update remote refs along with associated objects

其中遠程和本地的refs都儲存在本地的.git目錄之下,而且都只有一個refs:

遠程refs:

hyk@hyk-linux:~/xfstests/.git (master) 
$ cat refs/remotes/origin/master 
56a3959a96f1b5e046b3760778fd34b4911d0516

本地refs:

hyk@hyk-linux:~/xfstests/.git (master) 
$ cat refs/heads/master 
2c13da0b38b794581790ed0122a674d6ad6113ba

回顧我們原來學習過的分支建立的儲存模型,push的實質是進行commit對象在遠端建立和指標的更新問題。也就是說,如果使用者乙在此時push,那麼X後代會指向B,B的parent也指向X,然後此時我們的X後代已經指向了A,所以會失敗


2.方案一:強制覆蓋


在某些情況下,你需要對別人的提交(也可能是你自己的),進行強制覆蓋。例如,下面這種情況:

There is another common situation where you may encounter non-fast-forward rejection when you try to push, and  it is possible even when you are pushing into a repository nobody else pushes into. After you push commit A yourself (in the first picture in this section), replace it with "git commit --amend" to produce commit B, and you try to push it out, because forgot that you have pushed A out already. 

此時,我們只需要執行命令;git push --force


3.方案二:形成merge形式的提交曆史


這是最常見的一種情況,如果你不想丟失自己的工作,也不想丟失別人的工作(你需要同時儲存從X到A和B和提交曆史)在每次push之前,我們先從伺服器上pull最新版本(這樣,就更新了本機存放區的 refs/remotes/origin/master ),處理衝突,然後進行push,就不會產生問題了。此時,形成的提交曆史如下:

                 B---C
                /     /
            ---X---A


4.方案三:形成線性提交曆史


很多情況下,為了保持提交曆史的整潔,我們需要形成線性提交曆史。仍然以這個例子說明:我們可以提取X和B之間的diff,把這個diff應用給A,形成如下的提交曆史:

------X-------A--------D(the diff of X and B)

此時,需要的命令是:git push --rebase


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.