git学习(6)多人协作

git学习(6)多人协作

当我们从远程仓库克隆的时候,git会自动的把本地的master和远程的master对应起来,并且远程仓库的默认名称是origin

查看远程库的信息

$ git remote
origin

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

推送分支的时候,添加上分支的名称,就能把分支推送到远程库对应的远程分支上

$ git push orignin master
$ git push origin dev

如果远程没有该分支会自动添加,但是如果dev存在,且你的git目录是从origin clone来的,那么你需要自己建立分支,语法 $ git checkout -b dev origin/dev,如果是自己建立dev分支提交到远程dev分支的是还需要自己建立Track,语法$ git branch --set-upstream=origin/dev

一步一步来看,首先先看不小心自己建立dev 需要添加Track

$ git clone git@github.com:KeithMorning/gitStudy.git
Cloning into 'gitStudy'...
remote: Counting objects: 64, done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 64 (delta 19), reused 59 (delta 14)
Receiving objects: 100% (64/64), 5.44 KiB | 0 bytes/s, done.
Resolving deltas: 100% (19/19), done.
Checking connectivity... done.
//克隆成功
$ ls
gitStudy
$ cd gitStudy
$ ls
currentwork.txt	newDev.txt	readme.txt
/*建立新分支*/
$ git checkout -b dev
Switched to a new branch 'dev'

/*vim新建文件goodstudy.h*/
$ ls
currentwork.txt	goodstudy.h	newDev.txt	readme.txt

/*当前dev分支提交修改*/
$ git add goodstudy.h
$ git commit -m "add new code on dev"
[dev 114cffc] add new code on dev
 1 file changed, 1 insertion(+)
 create mode 100644 goodstudy.h

好了现在我们push一下试试

$ git push origin dev
To git@github.com:KeithMorning/gitStudy.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:KeithMorning/gitStudy.git'
hint: Updates were rejected because the tip of your current branch is behind
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.

这里新建的dev和远程库的dev内容都是不一样的哦,跟我们新建一个devotherName是一样的,所以先pull一下吧

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> dev

git提醒我们没有当前的分支建立连接,因此可以通过最后的提示来修改当前分支和远程分支的联系,

$ git branch --set-upstream-to=origin/dev
Branch dev set up to track remote branch dev from origin.

我们在pull一下试试

$ git pull
Merge branch 'dev' of github.com:KeithMorning/gitStudy into dev
。。。

提醒我们将本地dev分支和远程dev分支合并,输入合并的原因,或者不输入取消合并,注意这个时候进入的是vim模式,点击i进入编辑模式,输入原因后,点击esc,然后输入:wq

$ git pull
Merge made by the 'recursive' strategy.
 code.m     | 1 +
 readme.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 code.m

好了现在可以push

$ git push origin dev
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 552 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
To git@github.com:KeithMorning/gitStudy.git
   032fc97..3611fbb  dev -> dev

如果一开始使用的是$ git checkout -b dev orign/dev会自动添加本地分支和远程分支分关系,需要做的就是完成本地分支修改后,提交后push就可以了

在上一步中我们使用pull的时候进行了一次分支合并,如果不幸出现合并的时候冲突怎么办呢

如A同学修改了readme.txt并已经做了提交和push,当我们也修改了readme.txt进行push的时候会发生冲突如

$ git push origin dev
To git@github.com:KeithMorning/gitStudy.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:KeithMorning/gitStudy.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.

我们需要先pull一下看看那

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2)
Unpacking objects: 100% (3/3), done.
From github.com:KeithMorning/gitStudy
   23e511f..98eae17  dev        -> origin/dev
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

好了现在打开冲突的文件readme.txt,可以看到冲突的地方进行修改后重新提交就可以了

最后付上外国友人制作的表单

posted @ 2015-01-19 04:47  KeithMorning  阅读(1346)  评论(0编辑  收藏  举报