Git — 多人协作、Rebase

多人协作

克隆远程库时,Git 自动把本地 master 分支和远程 master 分支对应起来,并且远程库默认名称是 origin

执行 git remotegit remote -v (显示更详细的信息,fetch 是抓取push 是推送,没有权限时看不到 push 的地址):

$ git remote
origin

$ git remote -v
origin  git@github.com:michaelliao/learngit.git (fetch)
origin  git@github.com:michaelliao/learngit.git (push)
git push 推送分支

Git 把分支推送到远程库对应的远程分支:

$ git push origin master

$ git push origin dev

可以推送的分支:

  • master:主分支,需要时刻与远程同步。
  • dev:开发分支,成员在上面工作,需要与远程同步。
  • bug:本地修复分支,不需要与远程同步。
  • feature:实验性分支,不需要与远程同步。
git clone 抓取分支

Git 从远程抓取远程库分支:

$ git clone git@github.com:xxxxx/gitskills.git
Cloning into 'learngit'...
remote: Counting objects: 40, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 40 (delta 14), reused 40 (delta 14), pack-reused 0
Receiving objects: 100% (40/40), done.
Resolving deltas: 100% (14/14), done.
冲突解决
  1. 当其他成员从远程库 clone 时,默认情况下,只能看到本地的 master 分支,可以执行 git brach 命令查:

    $ git branch
    * master
    
  2. 其他成员在 dev 分支上开发,必须创建远程 origindev 分支到本地,并在 dev 上继续修改且频繁提交 dev 分支到远程:

    $ git checkout -b dev origin/dev
    
    $ git add env.txt
    
    $ git commit -m "add env"
    [dev 7a5e5dd] add env
     1 file changed, 1 insertion(+)
     create mode 100644 env.txt
    
    $ git push origin dev
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To github.com:xxxxx/gitskills.git
       f52c633..7a5e5dd  dev -> dev
    
  3. 当你试图修改相同文件并推送时将会发生冲突

    $ cat env.txt
    env
    
    $ git add env.txt
    
    $ git commit -m "add new env"
    [dev 7bd91f1] add new env
     1 file changed, 1 insertion(+)
     create mode 100644 env.txt
    
    $ git push origin dev
    To github.com:xxxxx/gitskills.git
     ! [rejected]        dev -> dev (non-fast-forward)
    error: failed to push some refs to 'git@github.com:michaelliao/learngit.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.
    
  4. 先执行 git 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 pull 失败(提示 no tracking information),原因是没有指定本地 dev 分支与远程 origin/dev 分支的链接,解决方法:

    $ git branch --set-upstream-to=origin/dev dev
    Branch 'dev' set up to track remote branch 'dev' from 'origin'.
    
  5. 然后提交,推送到远程分支

    $ git commit -m "fix env conflict"
    [dev 57c53ab] fix env conflict
    
    $ git push origin dev
    Counting objects: 6, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
    Total 6 (delta 0), reused 0 (delta 0)
    To github.com:xxxxx/gitskills.git
       7a5e5dd..57c53ab  dev -> dev
    
rebase

git rebase 命令是把本地未 push分叉提交历史整理成直线;目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比:

  1. 当用 git log 查看日志:

    $ git log --graph --pretty=oneline --abbrev-commit
    *   e0ea545 (HEAD -> master) Merge branch 'master' of github.com:michaelliao/learngit
    |\  
    | * f005ed4 (origin/master) set exit=1
    * | 582d922 add author
    * | 8875536 add comment
    |/  
    * d1be385 init hello
    ...
    
  2. 执行 git rebase 命令:

    $ git rebase
    First, rewinding head to replay your work on top of it...
    Applying: add comment
    Using index info to reconstruct a base tree...
    M	hello.py
    Falling back to patching base and 3-way merge...
    Auto-merging hello.py
    Applying: add author
    Using index info to reconstruct a base tree...
    M	hello.py
    Falling back to patching base and 3-way merge...
    Auto-merging hello.py
    
  3. 再次执行 git log 查看:

    $ git log --graph --pretty=oneline --abbrev-commit
    * 7e61ed4 (HEAD -> master) add author
    * 3611cfe add comment
    * f005ed4 (origin/master) set exit=1
    * d1be385 init hello
    ...
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值