第一步:建立git倉庫
(1)cd到你的本地項(xiàng)目根目錄下(也可以cd加空格再把項(xiàng)目的文件夾直接拖進(jìn)終端里,然后回車)
(2)執(zhí)行g(shù)it的初始化命令
git init
第二步:將項(xiàng)目的所有文件添加到倉庫中
git add .
如果想添加某個(gè)特定的文件,只需把.換成特定的文件名即可(注意:"add" 和 "." 要用空格隔開)
第三步:將add的文件commit到倉庫
git commit -m "創(chuàng)建倉庫"
第四步:去github上創(chuàng)建自己的Repository,創(chuàng)建頁面如下圖所示:
填寫項(xiàng)目名和項(xiàng)目描述(也可不寫描述),然后點(diǎn)擊下面的Create repository,就會進(jìn)入到類似下面的一個(gè)頁面,拿到創(chuàng)建的倉庫的https地址
第五步:將本地的倉庫關(guān)聯(lián)到github上
git remote add origin https://github.com/zlj-jeck/NewRepository.git
后面的https鏈接地址換成你自己的倉庫url地址,也就是上面紅框中標(biāo)出來的地址,如果不報(bào)錯(cuò),進(jìn)行下一步就好.
如果提示出錯(cuò)信息fatal: remote origin already exists.
解決辦法如下:
1、先輸入$ Git remote rm origin
2、再輸入$git remoteadd origin https://github.com/zlj-jeck/NewRepository.git 就不會報(bào)錯(cuò)了!
3、如果輸入$ git remote rm origin 還是報(bào)錯(cuò)的話,error: Could not remove config section 'remote.origin'. 我們需要修改gitconfig文件的內(nèi)容
4、找到你的github的安裝路徑,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
5、找到一個(gè)名為gitconfig的文件,打開它把里面的[remote "origin"]那一行
刪掉就好了!
第六步:上傳github之前,要先pull一下,執(zhí)行如下命令:
git pull origin master
如果不報(bào)錯(cuò),進(jìn)行下一步就好
如果出現(xiàn)報(bào)錯(cuò) fatal: Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository以及fatal: Could not read from remote repository.
如果是首次上傳到遠(yuǎn)程庫,不用管,直接進(jìn)行第七步上傳到遠(yuǎn)程庫(出現(xiàn)上面錯(cuò)誤的原因可能是你的遠(yuǎn)程庫上是空的,還沒東西呢你就執(zhí)行g(shù)it pull下載命令,當(dāng)然會報(bào)錯(cuò),人家那是告訴你遠(yuǎn)程庫沒東西),如果不是首次上傳,則需要重新輸入$git remoteadd
origin https://github.com/zlj-jeck/NewRepository.git重新操作一遍去第五步找原因吧.
第七步,也就是最后一步,上傳代碼到github遠(yuǎn)程倉庫
git push -u origin master
執(zhí)行完后,如果沒有異常,等待執(zhí)行完就上傳成功了,中間可能會讓你輸入U(xiǎn)sername和Password,你只要輸入github的賬號和密碼就行了