1、github上創(chuàng)建自己的Repository:
Repository.png
2速警、填寫項(xiàng)目名稱叹誉、描述、選擇是否公開闷旧、需不需要?jiǎng)?chuàng)建README长豁,注意這個(gè)頁面的地址,之后要用到忙灼。
Repository1.png
3匠襟、建立git倉庫
命令cd到你的本地項(xiàng)目根目錄下,執(zhí)行g(shù)it命令该园,此命令會(huì)在當(dāng)前目錄下創(chuàng)建一個(gè).git文件夾酸舍。
git init
4、將項(xiàng)目的所有文件添加到倉庫中, 注意這個(gè).是指當(dāng)前目錄下所有文件
git add .
5里初、將add的文件commit到倉庫
git commit -m "注釋"
6啃勉、將本地的倉庫關(guān)聯(lián)到github上
git remote add origin 第2步中的地址。
7双妨、上傳代碼到github遠(yuǎn)程倉庫
git push -u origin master
遇到一個(gè)問題淮阐,在第2步將readme選上之后,第7步就報(bào)錯(cuò):
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/Aimeexu/SimpleFactoryPattern'
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.
是因?yàn)檫h(yuǎn)程repository和我本地的repository沖突導(dǎo)致的刁品,而我在創(chuàng)建版本庫后泣特,在github的版本庫頁面點(diǎn)擊了創(chuàng)建README.md文件的按鈕創(chuàng)建了說明文檔,但是卻沒有pull到本地挑随。這樣就產(chǎn)生了版本沖突的問題,所以第7步前需要先執(zhí)行下面這句:
git pull origin master --allow-unrelated-histories
這樣就好了状您。
貌似還有其他方法(我沒試)
- 使用強(qiáng)制push的方法:
$ git push -u origin master -f
這樣會(huì)使遠(yuǎn)程修改丟失,一般是不可取的兜挨,尤其是多人協(xié)作開發(fā)的時(shí)候膏孟。
- 若不想merge遠(yuǎn)程和本地修改,可以先創(chuàng)建新的分支:
$ git branch [name]
然后push
$ git push -u origin [name]
然后就沒有然后了暑劝。骆莹。。