創(chuàng)建遠(yuǎn)程倉(cāng)庫(kù)
登陸 GitHub泛鸟,然后選擇 Repositories 并點(diǎn)擊 New,輸入 Repository name, 點(diǎn)擊 Create repository 即可創(chuàng)建一個(gè)新的遠(yuǎn)程倉(cāng)庫(kù)。
Clone遠(yuǎn)程倉(cāng)庫(kù)到本地
git clone git@github.com:anyangxaut/LearnGit.git
效果圖如下所示:
$ git clone git@github.com:anyangxaut/LearnGit.git
Cloning into 'LearnGit'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
將一個(gè)已有的本地倉(cāng)庫(kù)與 github 倉(cāng)庫(kù)進(jìn)行關(guān)聯(lián)
git remote add origin git@github.com:anyangxaut/LearnGit.git
遠(yuǎn)程倉(cāng)庫(kù)的默認(rèn)名稱是origin。
把代碼 push 到遠(yuǎn)程倉(cāng)庫(kù)
git push -u origin master
新建遠(yuǎn)程倉(cāng)庫(kù)并第一次推送 master 分支時(shí)油啤,加上 -u 參數(shù),Git 不但會(huì)把本地的 master 分支內(nèi)容推送的遠(yuǎn)程新的 master 分支蟀苛,還會(huì)把本地的 master 分支和遠(yuǎn)程的 master 分支關(guān)聯(lián)起來(lái)益咬,此后提交代碼可以省去該參數(shù),即git push origin master
帜平。
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 294 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:anyangxaut/LearnGit.git
b9df407..77391b3 master -> master
Branch master set up to track remote branch master from origin.
查看遠(yuǎn)程倉(cāng)庫(kù)信息
git remote -v
效果圖如下所示:
$ git remote -v
origin git@github.com:anyangxaut/LearnGit.git (fetch)
origin git@github.com:anyangxaut/LearnGit.git (push)
把本地的某個(gè) branch push 到遠(yuǎn)程倉(cāng)庫(kù)
git push origin <branch name>
效果如下:
$ git branch newbranch
$ git branch
* master
newbranch
$ git push origin newbranch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:anyangxaut/LearnGit.git
* [new branch] newbranch -> newbranch
獲取遠(yuǎn)程修改到本地
git pull
效果如下所示:
$ git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From github.com:anyangxaut/LearnGit
77391b3..196661a master -> origin/master
77391b3..de923a8 newbranch -> origin/newbranch
Updating 77391b3..196661a
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)
本地創(chuàng)建并切換到遠(yuǎn)程分支對(duì)應(yīng)的分支
當(dāng)我們?cè)趃ithub中創(chuàng)建了一個(gè)新的分支originbranch后幽告,本地創(chuàng)建并切換到遠(yuǎn)程分支對(duì)應(yīng)的分支時(shí)我們會(huì)輸入以下命令:
git remote update
git fetch
git checkout -b branch-name origin/branch-name
效果如下所示:
$ git branch
* master
newbranch
$ git remote show origin
* remote origin
Fetch URL: git@github.com:anyangxaut/LearnGit.git
Push URL: git@github.com:anyangxaut/LearnGit.git
HEAD branch: master
Remote branches:
master tracked
newbranch tracked
originbranch new (next fetch will store in remotes/origin)
Local branch configured for 'git pull':
master merges with remote master
Local refs configured for 'git push':
master pushes to master (up to date)
newbranch pushes to newbranch (local out of date)
$ git remote update
Fetching origin
From github.com:anyangxaut/LearnGit
* [new branch] originbranch -> origin/originbranch
$ git fetch
$ git checkout -b originbranch origin/originbranch
Branch originbranch set up to track remote branch originbranch from origin.
Switched to a new branch 'originbranch'
$ git branch
master
newbranch
* originbranch