git 的多人协作
当你从远程仓库克隆时,实际上 Git 自动把本地的master
分支和远程的master
分支对应起来了,并且,远程仓库的默认名称是origin
。
每次合并再push后,分支变成了这样:
git log
$ git log --graph --pretty=oneline --abbrev-commit
* f4e7ea8 (HEAD -> master) Merge branch 'dev' into master
|\
| * ad728db (origin/dev, dev) fix env conflict
| |\
| | * d7e6866 Update env.txt
| * | 770ce3f new env
| |/
| * 34bde7f add env
| * 0944c8c fix bug 101
* | b003293 (origin/master) merged bug fix 101
|\ \
| * | 8842ff5 (issue-101) fix bug 101
|/ /
* | fc76cf7 merge with no-ff
|\|
| * c7b409a add merge
|/
* fb8b190 conflict fixed
总之看上去很乱,有强迫症的童鞋会问:为什么Git的提交历史不能是一条干净的直线?
其实是可以做到的!
Git有一种称为rebase的操作,有人把它翻译成“变基”。我们来看看 rebase 是怎么把分叉的提交变成直线。
在和远程分支同步后,我们对hello.py这个文件做了两次提交。用git log命令看看:
$ git log --graph --pretty=oneline --abbrev-commit
* 582d922 (HEAD -> master) add author
* 8875536 add comment
* d1be385 (origin/master) init hello
* e5e69f1 Merge branch 'dev'
|\
| * 57c53ab (origin/dev, dev) fix env conflict
| |\
| | * 7a5e5dd add env
| * | d7e68661 Update env.txt
...
注意到Git用(HEAD -> master)和(origin/master)标识出当前分支的HEAD和远程 origin 的位置分别是 582d922 add author和 d1be385 init hello,本地分支比远程分支快两个提交。
现在我们尝试推送本地分支:
$ git push origin master
To codechina.csdn.net:xiongjiamu/learning-git.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'codechina.csdn.net:xiongjiamu/learning-git.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
$ git pull
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 codechina.csdn.net:xiongjiamu/learning-git
d1be385..f005ed4 master -> origin/master
* [new tag] v1.0 -> v1.0
Auto-merging hello.py
Merge made by the 'recursive' strategy.
hello.py | 1 +
1 file changed, 1 insertion(+)
再用git status看看状态:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
加上刚才合并的提交,现在我们本地分支比远程分支超前3个提交。
用git log看看:
$ git log --graph --pretty=oneline --abbrev-commit
* 00f5a14 (HEAD -> master) Merge branch 'master' of codechina.csdn.net:xiongjiamu/learning-git
|\
| * 9dbfd41 (origin/master) set exit=1
* | 8789044 add author
* | 01ebb65 add comment
|/
* 3010226 init hello
...
对强迫症童鞋来说,现在事情有点不对头,提交历史分叉了。
这个时候,rebase就派上了用场。我们输入命令git rebase试试:
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
输出了一大堆操作,到底是啥效果?再用git log看看:
$ git log --graph --pretty=oneline --abbrev-commit
* 22a1da7 - (HEAD -> master) add author
* 00bae30 - add comment
* 9dbfd41 - (origin/master) set exit=1
* 3010226 - init hello
...
原本分叉的提交现在变成一条直线了!这种神奇的操作是怎么实现的?其实原理非常简单。我们注意观察,发现 Git 把我们本地的提交“挪动”了位置,放到了 9dbfd41 (origin/master) set exit=1 之后,这样,整个提交历史就成了一条直线。
rebase 操作前后,最终的提交内容是一致的,但是,我们本地的 commit 修改内容已经变化了,它们的修改不再基于 d1be385 init hello,而是基于 9dbfd41 (origin/master) set exit=1,但最后的提交 22a1da7 内容是一致的。
这就是rebase操作的特点:把分叉的提交历史“整理”成一条直线,看上去更直观。缺点是本地的分叉提交已经被修改过了。
最后,通过push操作把本地分支推送到远程:
git push
$ git push origin master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 565 bytes | 565.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0), pack-reused 0
To codechina.csdn.net:xiongjiamu/learning-git.git
9dbfd41..22a1da7 master -> master
再用git log看看效果:
* 22a1da7 (HEAD -> master, origin/master) add author
* 00bae30 add comment
* 9dbfd41 set exit=1
* 3010226 init hello
...
远程分支的提交历史也是一条直线。
以上就是 rebase 的详细介绍了,现在让我们来回顾一下:
- rebase操作可以把本地未push的分叉提交历史整理成直线
- rebase的目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比
到目前,关于使用 git 进行协作开发的基本内容都介绍完了。
评论区