使用git可以把我們的項目代碼上傳到github上面去,方便自己管理,如何使用git?覺得是每位程序猿所必需要有的謀生技能,所以在此記錄一下自己學會使用的這個過程:
一、需要注冊github賬號,這樣就可以在自己的github上面創建倉庫(Create a New Repository)了,填好一些配置信息,然后便可以點擊"Create Repository"按鈕了!http://t.cn/RUbL4rP如圖:
二、安裝客戶端tortoiseGit(小烏龜),於是我們便可以右鍵Git Init Here(初始化本地倉庫),然后會出現.git文件,同時也可以Git Bash進入git命令行,將項目代碼上傳至github上面創建的對應的倉庫。
三、配置Git
1、首先需要在本地創建ssh key(可以理解創建密鑰文件)
進入這個angular項目
$ git init //初始化
$ git add README.md //更新README文件
$ git commit -m 'first commit' //提交更新,並注釋信息“one commit”,第一次提交
$ git remote add origin git@github.com:xiaobin5201314/angular.git //第一次需要連接遠程github項目
$ git push -u origin master //將本地項目更新到github項目上去,或是(git push origin master)
如果輸入$ git remote add origin git@github.com:xiaobin5201314(github帳號名)/gitdemo(項目名).git
提示出錯信息:fatal: remote origin already exists.
解決辦法如下:
1、先輸入$ git remote rm origin
2、再輸入$ git remote add origin git@github.com:xiaobin5201314/gitdemo.git 就不會報錯了!
3、如果輸入$ git remote rm origin 還是報錯的話,error: Could not remove config section 'remote.origin'. 我們需要修改gitconfig文件的內容
4、找到你的github的安裝路徑,我的是C:\Users\ASUS\AppData\Local\GitHub\angular_d14f7551eeb4aea0e4ae9fcd3358bd96420bb5c8\etc
5、找到一個名為gitconfig的文件,打開它把里面的[remote "origin"]那一行
刪掉就好了!
如果輸入$ ssh -T git@github.com
出現錯誤提示:Permission denied (publickey).因為新生成的key不能加入ssh就會導致連接不上github,可以重新生成一個。
解決辦法如下:
1、先輸入$ ssh-agent,再輸入$ ssh-add ~/.ssh/id_key,這樣就可以了。
2、如果還是不行的話,輸入ssh-add ~/.ssh/id_key 命令后出現報錯Could not open a connection to your authentication agent.解決方法是key用Git Gui的ssh工具生成,這樣生成的時候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行來做。
3、最好檢查一下在你復制id_rsa.pub文件的內容時有沒有產生多余的空格或空行,有些編輯器會幫你添加這些的。
如果輸入$ git push origin master
提示出錯信息:error:failed to push som refs to .......
解決辦法如下:
1、先輸入$ git pull origin master //先把遠程服務器github上面的文件拉下來
2、再輸入$ git push origin master
3、如果出現報錯 fatal: Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository以及fatal: Could not read from remote repository.
4、則需要重新輸入$ git remote add origingit@github.com:xiaobin5201314/gitdemo.git