1. 程式人生 > >git push 失敗的處理方式

git push 失敗的處理方式

遠端建立了含有Readme的倉庫, 本地初始化並添加了遠端倉庫後, push卻失敗了, 出現提示:

$ git push origin dev

To https://git.oschina.net/erchoc/laradock.git

 ! [rejected]        dev -> dev (fetch first)

error: failed to push some refs to 'https://git.oschina.net/erchoc/laradock.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.

問題(Non-fast-forward)的出現原因是: git倉庫中已有一部分程式碼, 它不允許你直接把你的程式碼覆蓋上去。於是你有2個選擇方式:

1. 強推,即利用強覆蓋方式用你本地的程式碼替代git倉庫內的內容: git push -f

2. 先把git的東西fetch到你本地然後merge後再push

    - $ git fetch
    - $ git merge

這2句命令等價於

$ git pull 

可是, 有時候還會出現問題:

上面出現的 [branch "master"]是需要明確(.git/config)如下的內容

[branch "master"]

    remote = origin

    merge = refs/heads/master

這等於告訴git2件事:

1,當你處於master branch, 預設的remote就是origin。

2,當你在master branch上使用git pull時,沒有指定remote和branch,那麼git就會採用預設的remote(也就是origin)來merge在master branch上所有的改變

如果不想或者不會編輯config檔案的話,可以在bush上輸入如下命令列:

$ git config branch.master.remote origin 

$ git config branch.master.merge refs/heads/master 

之後再重新git pull下。最後git push你的程式碼吧。