本篇主要講解
1.本地倉庫提交到遠程倉庫
2.從遠程倉庫下載到本地倉庫
場景:前面的部分,已經完成了本地倉庫的全部工做,這適合局域網內開發,不適合遠程(外網)協同工做。所以,須要在外網(github)上也建立一個倉庫,實現本地倉庫與github上的倉庫通訊,實現數據同步,這樣github倉庫能夠做爲外網公共倉庫,供全部人同時開發使用。
#1.本地倉庫提交到遠程倉庫
##1.1github上創建倉庫
我這裏是在oschina的碼雲上新建一個倉庫(這個和github實質是同樣的),
##1.2本地添加遠程倉庫
在本地添加一個遠程倉庫,即本地倉庫的內容能夠提交到遠程倉庫,採用命令git
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git remote add origin https://git.oschina.net/lixianli/Test.git
用origin指代遠程倉庫https://git.oschina.net/lixianli/Test.git,之後用到遠程倉庫的地方均可以用origin代替,這個名字隨便取。有時候不止鏈接一個遠程倉庫,因此須要有多個名字,這裏我查看下鏈接了幾個遠程倉庫github
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git remote origin
很明顯,這裏只有一個遠程倉庫。
有時候咱們忘記了origin對應的遠程倉庫是哪個,採用以下命令查看fetch
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git remote -v origin https://git.oschina.net/lixianli/Test.git (fetch) origin https://git.oschina.net/lixianli/Test.git (push)
這裏咱們知道origin對應的遠程倉庫地址是https://git.oschina.net/lixianli/Test.git。
有時候咱們與某個遠程倉庫的合做結束了,則須要刪除這個遠程倉庫,採用以下命令.net
git remote rm 名稱
##1.3同步遠程倉庫
本地倉庫提交代碼到遠程倉庫前,須要同步一下遠程倉庫,即先更新一下遠程倉庫,使遠程倉庫保持最新版本,以下3d
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git pull origin master From https://git.oschina.net/lixianli/Test * branch master -> FETCH_HEAD * [new branch] master -> origin/master Already up-to-date.
##1.4提交本地倉庫內容到遠程倉庫
以下,它會提示你輸入git的帳號與密碼code
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git push origin master Username for 'https://git.oschina.net': lixianli19870606@126.com Password for 'https://lixianli19870606@126.com@git.oschina.net': Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://git.oschina.net/lixianli/Test.git cc278a2..0c03cfc master -> master sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
##1.5 git上查看結果
很明顯,本地倉庫的test.txt已經提交上來。
#2.從遠程倉庫下載到本地倉庫
#2.1 新建一個本地目錄
注意,這個新建的是目錄,不是倉庫。進入該目錄下圖片
sand@sand_pc MINGW64 /h/gitRepositories $ ls firstRepo/ secondRepo/ sand@sand_pc MINGW64 /h/gitRepositories $ cd secondRepo/ sand@sand_pc MINGW64 /h/gitRepositories/secondRepo $
由上可知,我新建的目錄是secondRepo。
#2.2 將遠程倉庫下載到本地開發
sand@sand_pc MINGW64 /h/gitRepositories/secondRepo $ git clone https://git.oschina.net/lixianli/Test.git Cloning into 'Test'... remote: Counting objects: 32, done. remote: Compressing objects: 100% (20/20), done. remote: Total 32 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (32/32), done. Checking connectivity... done.
現在,secondRepo目錄下會有clone下來的倉庫。rem
sand@sand_pc MINGW64 /h/gitRepositories/secondRepo $ ls Test/
倉庫名:Test
#2.3 提交本地倉庫到遠程倉庫
修改secondRepo目錄裏面的test.txt裏面的內容,再次提交到遠程。
修改內容以下get
sand@sand_pc MINGW64 /h/gitRepositories/secondRepo $ cd Test/ sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ ls README.md test.txt sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ cat test.txt 123456 789012 abcdef 111111
提交到本地
sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ git add test.txt sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ git commit -m "添加內容:111111" [master 12b343d] 添加內容:111111 1 file changed, 1 insertion(+)
提交到遠程
sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ git remote origin sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ git pull origin master From https://git.oschina.net/lixianli/Test * branch master -> FETCH_HEAD Already up-to-date. sand@sand_pc MINGW64 /h/gitRepositories/secondRepo/Test (master) $ git push origin master Username for 'https://git.oschina.net': lixianli19870606@126.com Password for 'https://lixianli19870606@126.com@git.oschina.net': Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 323 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://git.oschina.net/lixianli/Test.git 0c03cfc..12b343d master -> master
#2.4 查看遠程倉庫
由提交記錄就可知,提交成功了。