上一节我们,都是在本地进行管理,如何将本地的仓库推送到远程仓库呢:
在github上先建立一个仓库,创建README文件不要勾选,因为一旦创建就失去了和本地仓库的整合性。
创建完成后,出现下面的选项:
1. 如果你没有仓库,那么可以在本地创建一个,然后退送到网上。
2. 如果你有本地仓库,可以和这个整合。并给出了具体命令
将本地仓库推送到远程仓库
在网页上创建完仓库后不能做任何操作,不然会有版本操作的记录,将本地库推送的时候会发生错误。只能远端删除重建。
加入-u参数:将origin 仓库的master分支设置为本地仓库的当前分支的upstream(上游),添加这个参数后,本地仓库运行git pull时可直从origin仓库的master分支获取内容,不用添加其他参数再。
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git remote add origin git@github.com:x695/helloworld.git
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git branch
feature-A
feature-B
feature-C
* master
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
To git@github.com:x695/helloworld.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:x695/helloworld.git'
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.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
#在远端删除仓库,进行重建
$ git remote add origin git@github.com:x695/helloworld.git
fatal: remote origin already exists.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git push -u origin master
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (20/20), 1.56 KiB | 0 bytes/s, done.
Total 20 (delta 4), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$
将其他分支推送到远程仓库
- 本地创建一个feature-D分支
- 将这个分支推送到远程仓库
$ git checkout -b feature-D
Switched to a new branch 'feature-D'
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ git push -u origin feature-D
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
* [new branch] feature-D -> feature-D
Branch feature-D set up to track remote branch feature-D from origin.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$
从远程仓库获取别人的仓库并进行维护
获取master仓库
刚才我们本地创建了一个分支D并push到远程仓库,那么假如有另外一个开发者想维护这个分支,该如何实现:
- 另外一个同事新建一个本地仓库,git clone 这个远程仓库
$ cd
Administrator@YOUNG-PC MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@YOUNG-PC MINGW64 ~
$ mkdir clone_helloworld
Administrator@YOUNG-PC MINGW64 ~
$ cd clone_helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ git clone git@github.com:x695/helloworld.git
Cloning into 'helloworld'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (6/6), done.
Receiving objects: 100% (20/20), done.
Resolving deltas: 100% (4/4), done.
remote: Total 20 (delta 4), reused 20 (delta 4), pack-reused 0
Checking connectivity... done.
#拉取完成后,并没有进入到这个仓库,所以当期提示信息看不出是在master下
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ git branch
fatal: Not a git repository (or any of the parent directories): .git
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ ls
helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ pwd
/c/Users/Administrator/clone_helloworld
#进入到刚才克隆的仓库,会提示在master分支下
$ cd helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ ls
READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ ls -la
total 5
drwxr-xr-x 1 Administrator 197121 0 三月 12 06:40 ./
drwxr-xr-x 1 Administrator 197121 0 三月 12 06:39 ../
drwxr-xr-x 1 Administrator 197121 0 三月 12 06:40 .git/
-rw-r--r-- 1 Administrator 197121 74 三月 12 06:40 READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ git branch
* master
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
# 加入参数a能看到远程仓库的信息
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/feature-D
remotes/origin/master
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$
获取分支仓库D
$ git checkout -b feature-D origin/feature-D
Branch feature-D set up to track remote branch feature-D from origin.
Switched to a new branch 'feature-D'
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$
维护本地分支D,并提交
$ vi READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ cat READM.md
hello world !
#get some diff
-feature-A
-feature-B
-feature-C
-feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ git diff
diff --git a/READM.md b/READM.md
index 8531ab0..996edda 100644
--- a/READM.md
+++ b/READM.md
@@ -4,3 +4,4 @@ hello world !
-feature-A
-feature-B
-feature-C
+ -feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
#直接提交,并推送到远程仓库
$ git commit -am "Add feature-D"
[feature-D a4c023b] Add feature-D
1 file changed, 1 insertion(+)
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
1c6a091..a4c023b feature-D -> feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$
将别人的更改的分支(即线上最新的分支)拉取到本地
#项目创始人重新切换到原来的仓库上,分支D上获取别的开发者最新的更改
$ cd HelloWorld/
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ git pull origin feature-D
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:x695/helloworld
* branch feature-D -> FETCH_HEAD
1c6a091..a4c023b feature-D -> origin/feature-D
Updating 1c6a091..a4c023b
Fast-forward
READM.md | 1 +
1 file changed, 1 insertion(+)
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ cat READM.md
hello world !
#get some diff
-feature-A
-feature-B
-feature-C
-feature-D
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$