github 遠程倉庫操做

工做中須要在github上保存項目,一個倉庫中有多個分支,進行一些實驗,方便後面操做。html

參考連接

http://rogerdudler.github.io/git-guide/index.zh.htmlgit

http://www.ruanyifeng.com/blog/2014/06/git_remote.htmlgithub

一、建立遠程倉庫

我在github帳戶上建立了一個git_test的倉庫。服務器

建立完成倉庫以後,github會顯示一些git的基本操做。app

... or create a new repository on the command line

echo "# git_test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:TonySudo/git_test.git
git push -u origin master

... or push an existing repository from the command line

git remote add origin git@github.com:TonySudo/git_test.git
git push -u origin master

倉庫建好了,如何上傳文件呢?我知道的有兩種方法:ide

  • 將github上的倉庫clone到本地,添加文件以後再push。fetch

  • 在本地建立倉庫,鏈接到遠程的倉庫,而後再push文件。ui

下面分別介紹。.net

二、克隆遠程倉庫到本地

# clone的時候,遠程主機自動被命名爲origin。
$ git clone git@github.com:TonySudo/git_test.git
Cloning into 'git_test'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

# 克隆某個分支, 使用-b選項,進行分支的選擇,branch是遠端倉庫分支的名字。
$ git clone -b branch https://github.com/TonySudo/git_test.git
Cloning into 'git_test'...
Unpacking objects: 100% (6/6), done.packing objects:  16% (1/6)

remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Checking connectivity... done.

# 上傳文件

$ cd git_test/

# 建立文件
$ touch master

$ git add master

$ git commit -m "init push"
[master (root-commit) 436b48f] init push
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master
 
# 上傳到github 

$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 203 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:TonySudo/git_test.git
 * [new branch]      master -> master
 
上傳完成,在github的倉庫中就會看到一個master文件。

三、本地倉庫鏈接遠程倉庫

# 在本地建立一個目錄用於當作倉庫。

$ mkdir git

$ cd git

# 初始化本地倉庫

$ git init
Initialized empty Git repository in C:/Users/Tony/Desktop/git/git/.git/

# 鏈接遠程倉庫

$ git remote add origin git@github.com:TonySudo/git_test.git

# origin 是定義的遠程主機的名字, origin 是遠程倉庫的網址

若是git remote鏈接時出現錯誤

fatal: remote origin already exists.

解決方法參考:

http://blog.csdn.net/dengjianqiang2011/article/details/9260435

$ git remote rm origin 

再次運行以前的命令就能夠成功。

# 查看遠程倉庫的名字:

$ git remote -v
origin  git@github.com:TonySudo/git_exe.git (fetch)
origin  git@github.com:TonySudo/git_exe.git (push)

# 上傳文件

$ touch master

$ git add master

$ git commit -m "init push"
[master (root-commit) f88dd2b] init push
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master

$ git push -u origin master

若是出現錯誤。
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

$ git push --set-upstream origin master

4. 提取服務器上的更新

# 從服務器上下載更新,這只是下載下來,沒有對源碼進行更改。
#  默認取回全部的更新。
$ git fetch origin

# 取回某一個分支的更新,branch1是分支名,能夠是master或者其餘的。
$ git fetch origin branch1

# 將fetch下來的跟新和本地的分支進行合併。merge以後,本地的源碼纔會改變。
$ git merge

# git pull至關於執行了git fetch和git merge
# 將遠程的branch1分支的內容下載到本地的master分支。
# 也能夠將branch1更改成其餘的,例如master. 
$ git pull origin branch1:master

五、文件上傳

# 將本地的分支branch上的更新上傳到遠端的master分支。
$ git push origin branch:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 200 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:TonySudo/git_test.git
 * [new branch]      master -> master
有時候會出錯,說明本地上的文件跟服務器上的不一樣步 
$ git push origin branch:master
To git@github.com:TonySudo/git_test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:TonySudo/git_test.git'
hint: Updates were rejected because the tip of your current branch is behi
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方法,先將服務器上的更新先下載到本地,這樣就和服務器上的同步了,再進行提交便可。

將本地的branch分支的內容傳送大遠端倉庫的branch分支
$ git push origin branch:branch
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 255 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@github.com:TonySudo/git_test.git
 * [new branch]      branch -> branch

6. 分支

建立一個名爲branch1的分支code

git branch branch1

切換到分支branch1

git checkout branch1

將本地master分支的文件上傳到遠端倉庫的名爲branch1的分支上。 若是遠端這個分支不存在,就會建立這個分支。

git push origin master:branch1

Tony Liu

2017-2-8, Shenzhen

相關文章
相關標籤/搜索