在本地創建了一個Git倉庫后,還想在Github創建一個Git倉庫,並使其遠程同步。
1.在電腦的用戶主目錄下有無.ssh目錄,若有看是否有id_rsa和id_rsa.pub文件。若無,則創建SSH Key。打開Git Bash;
$ssh-keygen -t rsa -C "youremail@example.com"
2.在GitHub中的Account setting,SSH Keys頁面,點“Add SSH Key”,填上任意Title,在Key文本框里粘貼 id_rsa.pub文件的內容。
3.登錄Github后點擊 Create a new repository,創建一個新倉庫,並命名如:test。
4.根據GitHub提示,在本地test倉庫運行命令:
$git remote add origin git@github.com:xxxxx/test.git
注意:要將xxxxx換成自己的Github賬戶名,否則推送不上。
5.將本地庫所有內容推送到遠程庫上:
$ git push -u origin master Counting objects: 19, done. Delta compression using up to 4 threads. Compressing objects: 100% (19/19), done. Writing objects: 100% (19/19), 13.73 KiB, done. Total 23 (delta 6), reused 0 (delta 0) To git@github.com:michaelliao/learngit.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
此后,只要本地做了提交,就能通過命令:
$git push origin master
即要關聯一個遠程庫,使用命令git remote add origin git@server-name:path/repo-name.git;
關聯后,使用命令git push -u origin master第一次推送master分支的所有內容;
此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;
源自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013752340242354807e192f02a44359908df8a5643103a000
設置ssh后還需要用戶名和密碼:
遇到此問題說明你用的是https而不是ssh。所以,你可以更新origin:
git remote remove origin
git remote add origin git@github.com:Username/Your_Repo_Name.git
對於https方式可以在~/.netrc
文件里設定用戶名密碼,不過這樣的風險在於密碼是明文存放在這個文件里的,比較容易泄露
machine github.com login Username password Password