前面的博文簡單介紹了Git的基本知識和常見用法,鏈接如下:
- 《Git基礎知識之內部狀態管理系統》 https://www.cnblogs.com/phillee/p/14598437.html
- 《Git 基本操作之簡明指南》 https://www.cnblogs.com/phillee/p/14516406.html
- 《Git基本操作之遠程倉庫下載與保存、本地倉庫初始化並推送》 https://www.cnblogs.com/phillee/p/10610687.html
- 《Git基本操作之Terminal命令行新建倉庫並推送到遠程倉庫》 https://www.cnblogs.com/phillee/p/14308137.html
- 《Git基本操作之項目自定義配置、游離分支保存、忽略規則、本地與遠程倉庫同步及代理服務器問題》 https://www.cnblogs.com/phillee/p/11497726.html
- 《通過Git在本地局域網中的兩台電腦間同步代碼》 https://www.cnblogs.com/phillee/p/15353020.html
0.前言
git中有一些如果操作不當容易出大問題的命令,例如git push -f
。git push -f
是一個需要特別謹慎對待的指令。因為它完全無視先來后到的規則,會將提交歷史一並覆蓋掉。
非特殊情況不建議使用,每次使用前請務必確認你知道自己在做什么,保持頭腦清醒。
1.基本使用方法
有時候倉庫commit的歷史記錄真的太亂了,想要借助rebase指令好好整理一下,因為它修改的是已經發生的事實,正常來說是無法推送的,而使用強制推送就能解決這個問題。
基本語法如下
git push <遠程主機名> <本地分支名>
如果想將本地的 master 分支推送到 origin 主機的 master 分支,可以使用以下指令
$ git push origin master # 等價於:git push origin master:master
如果本地版本與遠程版本有差異,但又要強制推送,使用 --force 參數:
$ git push --force origin master
再一次強調:除非只有自己一個人用,不然不建議使用git push --force
。
2.有可能出現的問題
2.1 non-fast-forward
錯誤
這不,我在前幾天使用時就出現了一點小問題,不過也沒造成什么影響,解決方案一並附上。當時是想進行局域網中不同機器之間的代碼同步,詳見 通過Git在本地局域網中的兩台電腦間同步代碼。
問題描述
cv:mylocalrepo.git cv$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To file:///Users/cv/misc_codes/myrepo.git
! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'file:///Users/cv/misc_codes/myrepo.git'
解決方案
進入中轉倉庫,查看 config 信息,[receive] 一欄的denyNonFastforwards = true
是默認值,可以手動修改為false。
這時再在本機或者另外一台機器的克隆倉庫進行push就沒有問題了。
(全文完)
參考資料
[1] git push rejected: error: failed to push some refs https://stackoverflow.com/questions/9832348/git-push-rejected-error-failed-to-push-some-refs
[2] Denying non-fast-forward and SourceForge https://pete.akeo.ie/2011/02/denying-non-fast-forward-and.html
本文作者 :phillee
發表日期 :2021年9月29日
本文鏈接 :https://www.cnblogs.com/phillee/p/15352893.html
版權聲明 :自由轉載-非商用-非衍生-保持署名(創意共享3.0許可協議/CC BY-NC-SA 3.0)。轉載請注明出處!
限於本人水平,如果文章和代碼有表述不當之處,還請不吝賜教。
感謝您的支持
微信支付