这是我参与「第五届青训营 」笔记创作活动的第5天
这一期主要是因为git操作出过问题,所以来整理一下。
rebase 和 merge
merge
- git-merge命令是用于从指定的commit(s)合并到当前分支的操作。
- git-merge命令有以下两种用途:
- 用于git-pull中,来整合另一代码仓库中的变化(即:
git pull = git fetch + git merge) - 用于从一个分支到另一个分支的合并
- 用于git-pull中,来整合另一代码仓库中的变化(即:
rebase
git-rebase 存在的价值是:对一个分支做「变基」操作,保持干净的commits记录
- 合并同一分支的多次提交
- 例如,合并前4次提交:
git rebase -i HEAD~4 - 这时候,会自动进入
vi编辑模式:通过修改前几次提交的message和commands(如下所示),让原来的多次提交合并成更简洁的。
s cacc52da add: qrcode s f072ef48 update: indexeddb hack s 4e84901a feat: add indexedDB floder s 8f33126c feat: add test2.js # Rebase 5f2452b2..8f33126c onto 5f2452b2 (4 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. #注意不要合并先前提交的东西,也就是已经提交远程分支的纪录。
- 例如,合并前4次提交:
- 分支合并
- master超前于自己的feature分支时,想在本地更新别人在master上的修改。
- git rebase操作:
git:(feature) git rebase master- 首先,
git会把feature1分支里面的每个commit取消掉; - 其次,把上面的操作临时保存成
patch文件,存在.git/rebase目录下; - 然后,把
feature分支更新到最新的master分支; - 最后,把上面保存的
patch文件应用到feature分支上;
- 首先,
- git rebase是危险操作? 根据上文来看,
git-rebase很完美,解决了我们的两个问题:
1.合并commit记录,保持分支整洁;
2.相比merge来说会减少分支合并的记录; 但是git rebase会改变commit历史,假设A,B两个人都在feature上开发, A、B各自在本地有commit,假设Arebase了master,并且提交到远程仓库,那么当Bpull远程master的时候,就会丢失自己原来的提交记录。这就是为什么我们经常听到有人说git rebase是一个危险命令,因为它改变了历史,我们应该谨慎使用。
两者区别
这里是自己整理的区别,不一定完全准确。
- conflicts:
git merge是在merge的时候比较commits之后的结果与merge target branch的区别,解决一次冲突。git rebase是在将feature分支更新到最新的master上之后,每次自己的patch里的commit都会和最新的master节点解决一回冲突,所以是 。
reset 和 revert
下次再说,这回的字数凑够了。