工作中需要在github上保存項目,一個倉庫中有多個分支,進行一些實驗,方便后面操作。
參考鏈接
http://rogerdudler.github.io/git-guide/index.zh.html
http://www.ruanyifeng.com/blog/2014/06/git_remote.html
1、創建遠程倉庫
我在github賬戶上創建了一個git_test的倉庫。
創建完成倉庫之后,github會顯示一些git的基本操作。
... 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
倉庫建好了,如何上傳文件呢?我知道的有兩種方法:
-
將github上的倉庫clone到本地,添加文件之后再push。
-
在本地創建倉庫,連接到遠程的倉庫,然后再push文件。
下面分別介紹。
2、克隆遠程倉庫到本地
# 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文件。
3、本地倉庫連接遠程倉庫
# 在本地創建一個目錄用於當做倉庫。
$ 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
5、文件上傳
# 將本地的分支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的分支
git branch branch1
切換到分支branch1
git checkout branch1
將本地master分支的文件上傳到遠端倉庫的名為branch1的分支上。 如果遠端這個分支不存在,就會創建這個分支。
git push origin master:branch1
Tony Liu
2017-2-8, Shenzhen