推送本地項(xiàng)目到github

1.github創(chuàng)建new repository

  • 輸入repository name
  • 描述
  • 初始化README.md文件
  • 添加.gitignore,可以在下拉框中選擇你的項(xiàng)目類型,比如Java,則可以獲得針對(duì)性的忽略文件.
  • 添加協(xié)議
  • 創(chuàng)建repository

2.新建ssh key

    1. Open Git Bash
    1. Paste the text below, substituting in your GitHub email address.
ssh-keygen -t rsa -b 4096 -C "*your_email@example.com*"

This creates a new ssh key, using the provided email as a label.

 Generating public/private rsa key pair.
    1. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
Enter a file in which to save the key (/c/Users/*you*/.ssh/id_rsa):*[Press enter]*
Enter passphrase (empty for no passphrase): *[Type a passphrase]*
Enter same passphrase again: *[Type passphrase again]*

3.添加你的ssh key到ssh-agent

  • 1.確保ssh-agent正在運(yùn)行
# start the ssh-agent in the background
eval $(ssh-agent -s)
Agent pid 59566
    1. 添加你的ssh私鑰到ssh-agent.
ssh-add ~/.ssh/id_rsa

3.上傳公鑰到github

進(jìn)入github官網(wǎng),依次點(diǎn)擊頭像,Setting,SSH and GPG keys,然后點(diǎn)擊New SSH key


輸入title和.ssh/id_rsa.pub里面的值

點(diǎn)擊Add SSH key即可,重新登錄github賬戶,查看ssh key是否添加成功

4.本地確認(rèn)ssh key是否上傳成功

驗(yàn)證是否成功,在git bash下輸入

$ ssh -T git@github.com

5.新建本地項(xiàng)目

mkdir testpush
cd testpush

6.初始化git

git init

7.設(shè)置全局變量

接下來我們要做的就是把本地倉庫傳到github上去,在此之前還需要設(shè)置username和email,因?yàn)間ithub每次commit都會(huì)記錄他們

$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"

8.提交上傳

比如我上傳一個(gè)當(dāng)前文件夾下的src目錄和pom.xml文件

git add ./src/*
git add pom.xml
git commit -m "first commit"
git remote add origin %https://github.com/youproaddress.git%
git push -u origin master

%%中為你的項(xiàng)目地址.
還有一點(diǎn)需要注意,我們的項(xiàng)目url應(yīng)該選擇https協(xié)議的,選擇git協(xié)議的會(huì)提示沒有權(quán)限的錯(cuò)誤.
當(dāng)然了https協(xié)議的url每次提交都需要輸入用戶名和密碼.
而git協(xié)議的不需要,如何設(shè)置為git協(xié)議,后面會(huì)講.
當(dāng)然了,還是建議使用https協(xié)議,雖然麻煩一點(diǎn),但是更安全.

報(bào)錯(cuò):error: failed to push some refs to "%your repository's git url%"如何解決
當(dāng)要push代碼到git時(shí),出現(xiàn)提示:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/FDUAccessControlForBDA/LetUsFindIt.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.

問題(Non-fast-forward)的出現(xiàn)原因在于:git倉庫中已經(jīng)有一部分代碼,所以它不允許你直接把你的代碼覆蓋上去。于是你有2個(gè)選擇方式:

  • 強(qiáng)推,即利用強(qiáng)覆蓋方式用你本地的代碼替代git倉庫內(nèi)的內(nèi)容
git push -f
  • 先把git的東西fetch到你本地然后merge后再push.
$ git fetch
$ git merge

這2句命令等價(jià)于

$ git pull origin master

但是origin和master是需要設(shè)置的.

$ git config branch.master.remote origin 
$ git config branch.master.merge refs/heads/master 

之后再重新git pull,同步之后再git push你的代碼.

這時(shí)候又報(bào)錯(cuò)了:fatal: refusing to merge unrelated histories

git pull,因?yàn)楸镜貍}庫和遠(yuǎn)程倉庫中的內(nèi)容不同,發(fā)現(xiàn)refusing to merge unrelated histories,無法git pull
因?yàn)樗麄兪莾蓚€(gè)不同的項(xiàng)目,要把兩個(gè)不同的項(xiàng)目合并,git需要添加一句代碼,在git pull,這句代碼是在git 2.9.2版本發(fā)生的,最新的版本需要添加--allow-unrelated-histories

假如我們的源是origin,分支是master,那么我們 需要這樣寫git pull origin master --allow-unrelated-histories需要知道.


終于成功了!

參考文章

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 安裝 Github 查看是否安裝git: $ git config --global user.name "...
    Albert_Sun閱讀 13,852評(píng)論 9 163
  • 第一章 安裝Git工具 下載GitHub for Windows,直接點(diǎn)擊安裝,安裝完成后,可以看到“Git Sh...
    不圓的石頭閱讀 12,140評(píng)論 5 63
  • 呦呦呦呦呦 沒想到我們倆吵來吵去吵了三百多天了 剛剛看紀(jì)念日 看了這個(gè) 還有點(diǎn)小驚訝呢 爸爸竟然跟你在一起快一年了...
    郝紫倩閱讀 199評(píng)論 0 3
  • 一、基礎(chǔ)介紹: 正向解析:通過域名查找IP;反向解析:通過IP查找域名; 一、相關(guān)配置文件詳解 程序包:yum l...
    扎啤閱讀 1,658評(píng)論 0 1
  • 第三十二章 嘿嘿嘿 “誰啊,大早上的就打電話。”原玥把自己亂糟糟的雞窩頭埋進(jìn)了被窩里面,悶悶的聲音從被窩的縫隙中間...
    chief風(fēng)閱讀 606評(píng)論 0 0

友情鏈接更多精彩內(nèi)容