git遠程倉庫在本地看不到&push 到遠程倉庫失敗的解決方案


今天想把在公司寫的一些代碼上傳的github上,將本地倉庫和遠程倉庫關聯起來,執行: 【git push -u origin main 】 把本地main branch的代碼推送到遠程的main branch時,報錯:

  ! [rejected] main -> main (fetch first)
  error: failed to push some refs to 'github.com:hijack-621/tpr-website.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.

 
        

查資料發現是因為我們在本地新建庫后,與遠程倉庫的內容不一致導致的(遠程倉庫有一些內容本地沒有)。

而且去查看遠程分支:

Administrator@MS-TQHELRTLDMXE MINGW64 /d/phpstudy/WWW (develop)
$ git branch -a
* develop
  main
//只看到本地的分支!!!

 

 使用命令:git remote update origin --prune   --prune 去更新遠程分支

再次查看所有分支:

Administrator@MS-TQHELRTLDMXE MINGW64 /d/phpstudy/WWW (main)
$ git branch -a
  develop
* main
  remotes/origin/develop
  remotes/origin/main

但是執行 【git push -u origin main 】 仍舊報錯

! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:hijack-621/tpr-website.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

意思大致是:你現在的分支被隱藏了...

不明白為什么會被隱藏【是不是我開了兩個git bash 窗口, 用的同一個sshkey去操作的原因】,難怪 使用 -a  參數查看所有分支時,看不到遠程分支呢。。。=   》》 解決方案:

使用 --force 參數,強制去提交【完成命令  git push -u --force origin main 】【推送本地git中的文件到遠程的 main分支中】。【這里是 使用 gir push --help 】幫助命令時,在git 官方提供的 git-push manual page 頁面中找到的方案! 下面是官方的解釋!

--force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, 
when
--force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected. This flag disables these checks, and can cause the remote repository to lose commits; use it with care. Note that --force applies to all the refs that are pushed,
hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart).
To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).
See the <refspec>... section above for details.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM