文章參考:CSDN
連結:好幾個
作者:小天
商務合作:請加微信(QQ):2230304070
自學網站:www.startphp.cn
初學者總是不知道如何學習,我一直建議你要擁有一本屬於自己的學習課本,不管是基礎,還是提升。
抖音號:startphp
用短視頻和大家分享PHP學習方法,學習技巧與經驗分享,功能實例
歡迎關注抖音號:startphp
在工作中,你總是與代碼打交道,上傳代碼的時候,相信你不是用svn,就是用Git版本控制器,下面是最近這幾天整理下來經常會遇到的Git的一些錯誤的解決訪問,希望能幫助到你。
1 git pull遇到錯誤:error: Your local changes to the following files would be overwritten by merge:方法1:如果你想保留剛才本地修改的代碼,並把git伺服器上的代碼pull到本地(本地剛才修改的代碼將會被暫時封存起來)
git stash
git pull origin master
git stash pop如此一來,伺服器上的代碼更新到了本地,而且你本地修改的代碼也沒有被覆蓋,之後使用add,commit,push 命令即可更新本地代碼到伺服器了。
方法2、如果你想完全地覆蓋本地的代碼,只保留伺服器端代碼,則直接回退到上一個版本,再進行pull:
git reset
git pull origin master
2 在git push origin master時出現以下這個問題時:error: failed to push some refs to 'git@github.com:yangchao0718/cocos2d.git
hint: Updates were rejected because the tip of your current branch is behin
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.出現錯誤的主要原因是github中的README.md文件不在本地代碼目錄中 可以通過如下命令進行代碼合併【註:pull=fetch+merge]
git pull --rebase origin master
git push -u origin master
3 如果出現這樣的錯誤:The file will have its original line endings in your working directory.解決辦法:
git rm -r --cached ./
git config core.autocrlf false
git add ./
4 git出現這樣的錯誤:Git master branch has no upstream branch$> git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push原因分析:沒有將本地的分支與遠程倉庫的分支進行關聯。出現這種情況主要是由於遠程倉庫太多,且分支較多。在默認情況下,git push時一般會上傳到origin下的master分支上,然而當repository和branch過多,而又沒有設置關聯時,git就會產生疑問,因為它無法判斷你的push目標。
解決: 方法一:(遠程分支存在的情況才能使用)
# 查看要指向的 repository
git remote -v
# 查看所有分支
git branch -a
git push --set-upstream origin master
# master: 遠程branch
# oringin: 在clone遠程代碼時,git為你創建的指向這個遠程代碼庫的標籤,它指向repository。方法二:根據需要,替換origin和master,此方法的好處是即使遠程沒有你要關聯的分支,它也會自動創建一個出來,以實現關聯。
git push -u origin master
5 出現這個錯誤:! [rejected] master -> master (fetch first)! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@11.111.11.11:bboyHan/golang-data.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.原因分析:沒有同步遠程的master 解決:
git pull origin master
6 Git出現failed to push some refs to描述:
$ git push -u origin master
To git@github.com:******/Demo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.解決方法:
(1).使用強制push的方法:
$ git push -u origin master -f
這樣會使遠程修改丟失,一般是不可取的,尤其是多人協作開發的時候。
(2).push前先將遠程repository修改pull下來
$ git pull origin master
$ git push -u origin master
(3).若不想merge遠程和本地修改,可以先創建新的分支:
$ git branch [name]
然後push
$ git push -u origin [name]
7 Git出現fatal: refusing to merge unrelated histories的時候描述:這是從遠程庫pull項目,合併文件發生的異常
解決方案:在pull的時候添加 --allow-unrelated-histories。
$ git pull origin master ----allow-unrelated-histories這個功能是可以讓大家不要把倉庫上傳錯了,如果會加上這個代碼,那麼就是自己確定了上傳。之前很容易就把代碼傳錯了,現在可以看到,如果上傳的不是之前的,那麼就需要加代碼。
8 當Git出現error:src refspec master does not match any 錯誤時:描述:在push項目的時候,引發該異常。 原因分析:目錄中沒有文件,空目錄是不能提交上去的,獲取沒有add、commit文件直接進行push了。 解決方案:
touch README
git add README
git commit -m 'first commit'
git push origin master
9 git時出現fatal: Authentication failed for 'https://github.com/ ...描述:使用的https提交,在用SourceTree提交代碼時候發生錯誤,返回的錯誤提示說:fatal:Authentication failed for'https://github.com/... 解決方案:重新執行Git config命令配置用戶名和郵箱即可:
git config -–global user.name "xxx"
git config –-global user.email "xxx@xxx.com"以上分支看自己的而定,這裡都是以master為主
以上是文章的全部內容,有需要的可以關注以下公眾號,回復相應的關鍵詞就好了