跳到主要內容

有時候推不上去… Git push ... fail

偶爾在執行 Push 指令的時候會出現這個錯誤訊息:

$ git push
To https://github.com/eddiekao/dummy-git.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/eddiekao/dummy-git.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.
這段訊息的意思是線上版本的內容比你電腦裡這份還要新,所以 Git 不讓你推上去。

怎麼造成的?

通常這個狀況會發生在多人一起開發的時候,想像一下這個情境:



  1. Sherly 跟 Eddie 兩個人在差不多的時間都從 Git Server 上拉了一份資料下來準備進行開發。
  2. Sherly 手腳比較快,先完成了,於是先把做好的成果推一份上去。
  3. Eddie 不久後也完成了,但當他要推上去的時候發現推不上去了…

怎麼解決?

解決方法算是有兩招

第一招:先拉再推


因為你電腦裡的內容是比較舊的,所以你應該先拉一份線上版本的回來更新,然後再推一次:

$ git pull --rebase
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/eddiekao/dummy-git
   37aaef6..bab4d89  master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: update index

這裡加了 --rebase 參數是表示「內容抓下來之後請使用 Rebase 方式合併」,當然你想用一般的合併方式也沒問題。合併如果沒發生衝突,接著應該就可以順利往上推了。

第二招:無視規則,總之就是聽我的!(誤)

凡事總有先來後到,在上面的例子中,Sherly 先推上去的內容,後推的人就是應該拉一份下來更新,不然照規定是推不上去的。不過這規則也是有例外,只要加上了 --force 或是 -f 參數,它就會強迫硬推上去,把 Sherly 之前的內容蓋掉:

$ git push -f
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (19/19), 2.16 KiB | 738.00 KiB/s, done.
Total 19 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
To https://github.com/eddiekao/dummy-git.git
 + 6bf3967...c4ea775 master -> master (forced update)

雖然這樣一定會成功,但接下來你就要去面對同事,跟她解釋為什麼你把她的進度蓋掉了。

source : https://gitbook.tw/chapters/github/fail-to-push.html

留言

這個網誌中的熱門文章

Unofficial Windows Binaries for Python Extension Packages

You can find in url below : https://www.lfd.uci.edu/~gohlke/pythonlibs/ Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluorescence Dynamics, University of California, Irvine. Updated on 16 April 2020 at 22:50 UTC. This page provides 32- and 64-bit Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language. A few binaries are available for the PyPy distribution. The files are unofficial (meaning: informal, unrecognized, personal, unsupported, no warranty, no liability, provided "as is") and made available for testing and evaluation purposes. Most binaries are built from source code found on PyPI or in the projects public revision control systems. Source code changes, if any, have been submitted to the project maintainers or are included in the packages. Refer to the documentation of the individual packages for license restrictions and dependen...