代碼保存到Github上,供以後查看或者別人學習,是一件非常有意思的事情。那麼下面就來說說怎麼保存代碼到Github上。
註冊Github
這一步基本上對於程序員是必備。
ssh key 添加到github
本地生成ssh key
ssh-keygen -t rsa -C "email"
新建倉庫(Repository)
即在Github上新建一個自己的代碼倉庫,當然你可以創建多個,每個Repository裡放不同的代碼,注意圖中有兩個選項,add gitignore和add licence, 前者是根據選項把相關的目錄加入.gitigore文件,比如laravel,是將laravel框架中的一些目錄加入.gitigore, 後者是添加開源協議, 具體可搜索開源協議進行了解。如下圖所示:
下圖展示已創建的Repository,點擊clone or download可以查看Repository 地址。
本地初始化git
[www@yn-vm-dev29 test 22:35:21]$ git init
Initialized empty Git repository in /usr/www/test/.git/
克隆遠端倉庫到本地
git clone https://github.com/xxxx/test.git
Initialized empty Git repository in /usr/www/test/test/.git/
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
添加代碼到目錄
例如,touch hello.php
利用git命令提交
git status
git add filename
git commit -m 'xxxx'
git push origin master(git push)
此時應該會報錯:
error: The requested URL returned error: 403 Forbidden while accessing
https://github.com/yourgithubname/test.git/info/refs
fatal: HTTP request failed
解決方法:
打開當前目錄下.git/config文件
修改對應項如下(注意加粗部分):
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://yourgithubname@github.com/yourgithubname/test.git
然後再git push
此時會提示輸入github密碼,輸入之後,即可推送到遠端Repository。