iT邦幫忙

6

[Ting's筆記Day3] 解決Git常見錯誤 non-fast-forward問題

從上週開始,我就下了決心,打算每週寫3篇文章在IT邦!<握拳>
(理想且不偷懶的情況啦XD)
也是為了年底鐵人賽一口氣寫30篇文章馬拉松做準備:)
笨鳥趕快練習先飛

但據說,寫文章最難的部分,是找題材

所以最近每次遇到bug或錯誤都挺興奮 開心(?) 的,因為又可以寫筆記了
也可以記錄新手學習之路上遇到的常見問題,提供其他新手參考。

最近學Udemy平台Ruby學習線上課程,我在本機練習做了一個Ruby on Rails專案(名稱定為:yelpdemo),
https://ithelp.ithome.com.tw/upload/images/20180829/20111177kyLYw9hISV.png

我先用subl .叫出Sublime編寫專案內的網頁,做一點修改後,
再用Terminal按照我自己Day2筆記所說的Git步驟試著同步此專案到Github的同名repository儲存庫。

前情提要:我再Github的repo是上週設定好的,所以自己有點忘記當初發生什麼事~導致接下來的問題

Step1. 首先git status

看看目前working directory中所有檔案的情形,紅色代表這些檔案需要被追蹤:
https://ithelp.ithome.com.tw/upload/images/20180829/20111177faIYTaL67l.png

Step2. git add .

把以上所有檔案交給Git,再輸入一次git status,綠色代表已更改為被追蹤:
https://ithelp.ithome.com.tw/upload/images/20180829/20111177TwdEmf4DYw.png

Step3 git commit

git commit -m "first commit",備註-m裡寫上"執行第一個提交指令commit",將staged檔案儲存到儲存庫:
https://ithelp.ithome.com.tw/upload/images/20180829/20111177m0erHLYic9.png
然後再輸入一次git status
https://ithelp.ithome.com.tw/upload/images/20180829/20111177y4gEfjUE01.png
好了,到這邊都挺順的~

Step4.設定好要加入的路徑 git remote add origin

git remote add origin https://github.com/(你的帳號)/(你的專案名稱)
設定遠端origin的repository路徑,然後再推送:
git push -u origin master
-u是update的意思,用了參數-u之後,未來就能直接使用不帶參數的git pull從以前push過的分支來pull。

如果你沒有先git remote add origin就直接git push
就會像我一開始一樣,出現了'找不到儲存庫'的問題喔!
https://ithelp.ithome.com.tw/upload/images/20180829/20111177y7KBrSMI3T.png

Step5. git push -u origin master

[燈等!]更新被拒絕了,因為上週我在Remote建的的repo裡面,有些更動過的檔案版本,本地端並沒有:
https://ithelp.ithome.com.tw/upload/images/20180829/20111177ElauUZHXsa.png

[解決]提示告訴我可以先用git pull
https://ithelp.ithome.com.tw/upload/images/20180829/20111177Z2gTdZDmzg.png

然後再git push一次:

https://ithelp.ithome.com.tw/upload/images/20180829/20111177KVWSk3rmg5.png

重點在於最後一行:git push預設會推送「本地分支」到相同檔名的「遠端分支」

When push.default is set to 'matching', git will push local branchesto the remote branches that already exist with the same name.

[燈等!]問題再度出現:The current branch master has no upstream branch,表示當前分支主機沒有上游分支:
https://ithelp.ithome.com.tw/upload/images/20180829/201111776Dl8yTegA2.png

[解決]按照提示,依樣畫葫蘆地git push --set-upstream origin master

https://ithelp.ithome.com.tw/upload/images/20180829/20111177fhtIcNgZql.png

[燈等!] 新的問題產生,提示說我的master是non-fast forward

https://ithelp.ithome.com.tw/upload/images/20180829/201111776GMlki3aTG.png

這個non-fast forward 的意思是本機commit和遠端的不相同,本地和遠端的Repository發生文件衝突問題(線圖有分岔)。

由於上週在遠端創建的資料夾內容文件發生改變,因此不允許我把本地的文件覆蓋上去。

關於合併local端和remote端,近一步了解可以看git官網,或是連猴子都能懂的Git入門指南解釋

在push之前,我需要
git fetch
git merge
將遠端的改變用merge合併到本地上。

或是我可以用git push -f 強制覆蓋本地文件替代git儲存庫的內容。

Google關鍵字在stackoverflow找到的解法是
git push -f origin <branch>

-f代表force:強制推送。如果不想把遠端分支的更動合併(merge)到本地分支,則可以使用這個指令。但要小心使用。

[解決]
https://ithelp.ithome.com.tw/upload/images/20180829/20111177vYKA8TDUfV.png

回到github站上檢查,終於同步完成:

https://ithelp.ithome.com.tw/upload/images/20180829/201111779euCL1MqMF.png

===
後記:
這是寫給自己提升記憶的筆記。相信之後如果在本機再建立其他新專案、遠端建立儲存庫並使用gite功能,可能還是會遇到以上類似的問題,身為自學git新新手,我沒有直接發問求救,而是靠google增加觀念、進而找到解答,因此還蠻開心的!

看了資料,有些工程師的建議是少用git pull,以git fetchgit merge代替。也許以後實作之後有類似的經驗和心得,再寫點相關的文章。

ref:
https://blog.exfast.me/2016/05/git-instructions-instructions/
https://gitbook.tw/chapters/using-git/add-to-git.html


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
暐翰
iT邦大師 1 級 ‧ 2018-08-29 15:39:47

 我就下了決心,打算每週寫3篇文章在IT邦

支持一下!

有觀眾的回應真好!會讓我趕快起床想主題寫文章~~

0
Bater
iT邦新手 4 級 ‧ 2018-08-29 18:49:32

能自己解決問題,已經很有工程師的樣子囉!這樣的問題工作中偶爾也會發生,繼續加油吧。

感謝饅頭貓貓!

3
小碼農米爾
iT邦高手 1 級 ‧ 2018-08-30 03:43:30

小弟沒有用指令操作過 git 覺得蠻有趣的,分享一下研究心得。

Step5 的 git pull 其實沒有拉東西下來,是因為上一個 push 指令失敗,
沒有成功追蹤遠端 master,所以 git 不知道要 merge 哪一個遠端分支回當前分支上。

There is no tracking information for the current branch.

接著執行 git push,其實這裡的問題和上一個指令一樣,沒有追蹤遠端分支,
您的 git 版本應該是 2.0 以上,因此才會有這段錯誤訊息。

fatal: The current branch new has no upstream branch. 
To push the current branch and set the remote as upstream, 

    use git push --set-upstream origin master 

如果是舊版預設 push.default=matching 會推送所有分支,
而 2.0 以上預設 push.default=simple 如果沒有指定分支,就會出現上面的錯誤 [1]。

接著執行了 git push --set-upstream origin master,其實回到了開頭 XD。

git push -u origin master
//等於
git push --set-upstream origin master

不過怎麼訊息好像和第一段不太一樣 [2]。

  • fetch first: 表示遠端的版本和本地的 origin/master 不同。
  • non-fast forward: 表示當前 master 和 origin/master 不同。

因為 pull = fetch + merge 之前下過的 git pull 雖然失敗,但其實是出錯在 merge 的部分,fetch 已經將遠端的版本更新回 origin/master 了。

最後的 git push -f 如您所說 要小心使用,因為如果多人開發,改變遠端分支,可能會讓其他人的 git 壞掉 XD。

阿,好晚了... 明天再繼續研究,感謝大大分享,學到很多東西。
今天又熬夜了...
/images/emoticon/emoticon16.gif

參考文章

[1] Git push與pull的預設行為
[2] Git “fetch first”and ”non-fast-forward“ error when trying to push

哇!感謝大大的實際操作以及回應分享(拜)這樣交流整理的真好!
*等於是另外一篇blog文章了XD *

讓我也對git fetch有更多認識,下次我不用pull,取代用fetch + merge:)

哈哈哈,我很少用其他功能,偷懶只用 pushpull
/images/emoticon/emoticon16.gif

有時候想讓分支圖乾淨一點可以用。

fetch + rebase
//或
fetch + cherry-pick

因為 merge 會多一個合併節點,多人在推的時候分支圖會分出去一大堆線,
rebasecherry-pick 則不會,
有問過朋友不過好像沒有人在意這點,所以後來就放棄這件事 XD。

話說今年的鐵人賽要開始了,不是年底喔!!! 我也一直以為是年底。 /images/emoticon/emoticon17.gif

我要留言

立即登入留言