Git錯誤non-fast-forward後的衝突解決

當要push代碼到git時,出現提示: git

error:failed to push some refs to ... ide

Dealing with 「non-fast-forward」 errors
From time to time you may encounter this error while pushing: fetch

  1. $ git push origin master  
  2. To ../remote/  
  3.  ! [rejected]        master -> master (non-fast forward)  
  4. error: failed to push some refs to '../remote/'  
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.
In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

問題(Non-fast-forward)的出現緣由在於:git倉庫中已經有一部分代碼,因此它不容許你直接把你的代碼覆蓋上去。因而你有2個選擇方式: this

1,強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內的內容 spa

git push -f 命令行

2,先把git的東西fetch到你本地而後merge後再push orm

$ git fetch rem

$ git merge it

這2句命令等價於
  1. $ git pull  

但是,這時候又出現了以下的問題: io

上面出現的 [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上輸入以下命令行:

  1. $ git config branch.master.remote origin  
  2. $ git config branch.master.merge refs/heads/master  

以後再從新git pull下。最後git push你的代碼吧。it works now~

相關文章
相關標籤/搜索