因为在实际开发过程中被git的rebase搞晕了,于是系统性的恶补了下Git相关。本文只是一些进阶性且很实战性质的记录,向init、add、commit等基础的命令不在本文范畴之内。对于系统性的Git学习,我只推荐 ProGit2 中文版。官方的电子书!
一、暂存区、工作区与版本区
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
二、分支
HEAD指向当前分支
1 2 3 4 5 6 7 8 9 10
| git branch <name>
git checkout <name>
git branch -b <name>
git merge <name>
git branch -d <name>
|
三、远程分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git fetch <origin> //添加远程仓库名称 git remote add <origin> git@github.com:someaccount/someproject.git //修改远程仓库地址 git remote set-url <origin> git@github.com:someaccount/someproject.git //查看远程仓库 git remote -v //推送远程分支 git push <origin> <name> git push <origin> <localname>:<remotename> //删除远程分支 git push origin --delete <serverfix> //从远程拉取分支 并切过去 git checkout -b <sf> origin/<serverfix>
|
四、变基(rebase)
1、场景一
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
1 2 3 4 5 6
| git checkout experiment //将当前分支变基到master git rebase master git checkout master //将experiment合并到当前分支 git merge experiment
|
2、场景二
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
1 2 3 4
| //将client分支变基到master分支但是不包含与server分支共同的修改 git rebase --onto master server client git checkout master git merge client
|
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
1
| git rebase master server
|
五、现实工作流案例分析
我在实际开发过程中仓库中 release/9.4.0分支,本地拉取 release/9.4.0分支,然后写完需求,提交到远程的 feature/9.4.0_[name] 分支,然后从仓库上 feature/9.4.0_[name]合并到release/9.4.0。通常本地也就只有 release/9.4.0分支。
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
远程仓库提pull request,然后在远程仓库处理冲突,并合并。
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
1 2 3 4
| git checkout feature/9.4.0_tag2 git rebase release/9.4.0
git rebase –continue
|
六、Rebase VS Merge
综合上面的工作流,对比Rebase和Merge如下:
比较 |
rebase |
merge |
解冲突地点 |
本地解冲突 |
远程解冲突 |
是否会产生新的节点 |
不会 |
会 |
码农明明桑: git中merge和rebase的区别
七、Stash
场景:我想拉取某个分支release/9.4.0,但是我已经有改动,但暂时不想提拉,拉不下来
1 2 3 4 5 6 7 8
| //1、先暂存 可以指定名称,具体看 git stash 参数 git stash //2、拉下代码 git pull //3、恢复暂存,这时候可能会有冲突 git stash pop // 删除暂存 git stash clear
|
八、SourceTree操作对应命令
1、远程分支
2、本地分支
- 检出 checkout
- 合并 merge master 到当前分支(gitpro1)
- 变基 rebase master(远程的master)
3、全局界面
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQIHWNgAAIAAAUAAY27m/MAAAAASUVORK5CYII=)
九、其他
版本回退:针对已经提交但没有push到远程仓库的提交
1 2 3 4
| git reset
git log git reset
|