Git多人协作

来源:互联网 发布:p2p类似软件 编辑:程序博客网 时间:2024/05/22 08:11
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin。
要查看远程库的信息,用git remote:
$ git remoteorigin
或者,用git remote -v显示更详细的信息:
$ git remote -vorigin  git@github.com:morris131/gittest.git (fetch)origin  git@github.com:morris131/gittest.git (push)
上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

推送分支
推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:
$ git push origin master
如果要推送其他分支,比如dev,就改成:
$ git push origin dev


但是,并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?
master分支是主分支,因此要时刻与远程同步;
dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;
feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。
总之,就是在Git中,分支完全可以在本地自己藏着玩,是否推送,视你的心情而定!


抓取分支
多人协作时,大家都会往master和dev分支上推送各自的修改。
现在,模拟一个你的小伙伴,可以在另一台电脑(注意要把SSH Key添加到GitHub)或者同一台电脑的另一个目录下克隆(这里用不同目录代替):
1. 在e:\gittest下创建dev分支,添加hello.txt,并提交到远程版本库。
</pre><pre name="code" class="html">$ cd e:\gittest$ git branch* master$ git checkout -b devSwitched to a new branch 'dev'$ git statusOn branch devnothing to commit, working directory clean$ git push origin devTotal 0 (delta 0), reused 0 (delta 0)To git@github.com:morris131/gittest.git * [new branch]      dev -> dev$ git add hello.txt$ git commit -m "add hello"[dev 3d5aa8e] add hello 1 file changed, 1 insertion(+) create mode 100644 hello.txt$ git push origin devCounting objects: 3, done.Delta compression using up to 2 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:morris131/gittest.git   b0e9ec8..3d5aa8e  dev -> dev

2. 在d盘下克隆远程版本库,修改dev分支的hello文件并提交到远程版本库
$ cd d:\gittest$ lsreadme.txt$ git branch* master$ git checkout -b dev origin/devBranch dev set up to track remote branch dev from origin.Switched to a new branch 'dev'$ git branch* dev  master$ lshello.txt  readme.txt$ cat hello.txtaa$ vim hello.txt$ cat hello.txtbb$ git statusOn branch devYour branch is up-to-date with 'origin/dev'.Changes not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   hello.txtno changes added to commit (use "git add" and/or "git commit -a")$ git add hello.txt$ git commit -m "add hello bb"[dev db93ddf] add hello bb 1 file changed, 1 insertion(+), 1 deletion(-)$ git push origin deverror: unable to read askpass response from 'D:/Program Files/Git/mingw32/libexec/git-core/git-gui--askpass'Username for 'https://github.com': morris131Counting objects: 3, done.Delta compression using up to 2 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To https://github.com/morris131/gittest.git   3d5aa8e..db93ddf  dev -> dev

3. 在e盘下修改hello.txt尝试提交后有冲突,解决冲突后再提交到远程版本库
$ cd e:\gittest$ lshello.txt  readme.txt$ git branch* dev  master$ cat hello.txtaa$ vim hello.txt$ git statusOn branch devChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)        modified:   hello.txtno changes added to commit (use "git add" and/or "git commit -a")$ git add hello.txt$ git commit -m "hello cc"[dev c1b73d8] hello cc 1 file changed, 1 insertion(+), 1 deletion(-)$ git push origin devTo git@github.com:morris131/gittest.git ! [rejected]        dev -> dev (fetch first)error: failed to push some refs to 'git@github.com:morris131/gittest.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.$ git pullremote: Counting objects: 3, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), done.From github.com:morris131/gittest   3d5aa8e..db93ddf  dev        -> origin/dev   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 branch --set-upstream-to=origin/dev devBranch dev set up to track remote branch dev from origin.$ git push origin devTo git@github.com:morris131/gittest.git ! [rejected]        dev -> dev (non-fast-forward)error: failed to push some refs to 'git@github.com:morris131/gittest.git'hint: Updates were rejected because the tip of your current branch is behindhint: 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.$ git pullAuto-merging hello.txtCONFLICT (content): Merge conflict in hello.txtAutomatic merge failed; fix conflicts and then commit the result.$ cat hello.txt<<<<<<< HEADcc=======bb>>>>>>> db93ddfe008a997b691fd085eee841312bb93c71$ vim hello.txt$ cat hello.txtcc and bb$ git add hello.txt$ git commit -m "fixed hello"[dev c3423f3] fixed hello$ git push origin devCounting objects: 6, done.Delta compression using up to 2 threads.Compressing objects: 100% (4/4), done.Writing objects: 100% (6/6), 537 bytes | 0 bytes/s, done.Total 6 (delta 0), reused 0 (delta 0)To git@github.com:morris131/gittest.git   db93ddf..c3423f3  dev -> dev

0 0
原创粉丝点击