多人协作 - morris131/morris-book GitHub Wiki

当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin。 要查看远程库的信息,用git remote:

$ git remote

origin 或者,用git remote -v显示更详细的信息:

$ git remote -v

origin [email protected]:morris131/gittest.git (fetch)

origin [email protected]: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,并提交到远程版本库。

$ cd e:\gittest

$ git branch

  • master

$ git checkout -b dev

Switched to a new branch 'dev'

$ git status

On branch dev

nothing to commit, working directory clean

$ git push origin dev

Total 0 (delta 0), reused 0 (delta 0)

To [email protected]: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 dev

Counting 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 [email protected]:morris131/gittest.git

b0e9ec8..3d5aa8e dev -> dev 2. 在d盘下克隆远程版本库,修改dev分支的hello文件并提交到远程版本库

$ cd d:\gittest

$ ls

readme.txt

$ git branch

  • master

$ git checkout -b dev origin/dev

Branch dev set up to track remote branch dev from origin.

Switched to a new branch 'dev'

$ git branch

  • dev

    master

$ ls

hello.txt readme.txt

$ cat hello.txt

aa

$ vim hello.txt

$ cat hello.txt

bb

$ git status

On branch dev

Your branch is up-to-date with 'origin/dev'.

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

    modified:   hello.txt

no 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 dev

error: unable to read askpass response from 'D:/Program Files/Git/mingw32/libexec/git-core/git-gui--askpass'

Username for 'https://github.com': morris131

Counting 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

$ ls

hello.txt readme.txt

$ git branch

  • dev

    master

$ cat hello.txt

aa

$ vim hello.txt

$ git status

On branch dev

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

    modified:   hello.txt

no 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 dev

To [email protected]:morris131/gittest.git

! [rejected] dev -> dev (fetch first)

error: failed to push some refs to '[email protected]:morris131/gittest.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.

$ git pull

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0

Unpacking 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 

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

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

$ git branch --set-upstream-to=origin/dev dev

Branch dev set up to track remote branch dev from origin.

$ git push origin dev

To [email protected]:morris131/gittest.git

! [rejected] dev -> dev (non-fast-forward)

error: failed to push some refs to '[email protected]:morris131/gittest.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.

$ git pull

Auto-merging hello.txt

CONFLICT (content): Merge conflict in hello.txt

Automatic merge failed; fix conflicts and then commit the result.

$ cat hello.txt

<<<<<<< HEAD

cc

=======

bb

db93ddfe008a997b691fd085eee841312bb93c71

$ vim hello.txt

$ cat hello.txt

cc and bb

$ git add hello.txt

$ git commit -m "fixed hello"

[dev c3423f3] fixed hello

$ git push origin dev

Counting 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 [email protected]:morris131/gittest.git

db93ddf..c3423f3 dev -> dev

⚠️ **GitHub.com Fallback** ⚠️