此篇介绍Git rebase等操作。
 
Start git commit 1. git commit –amend  
1 2 3 4 5 6 7 8 9 10 11 12 ➜  git-exercise git:(main) ✗ git add . ➜  git-exercise git:(main) ✗ git commit -m "开始rebase,先测commit撤销"  [main b00c892] 开始rebase,先测commit撤销  1 file changed, 2 insertions(+)  create mode 100644 rebase1.txt ➜  git-exercise git:(main) git log  ➜  git-exercise git:(main) git log               ➜  git-exercise git:(main) ✗ git add rebase1.txt  ➜  git-exercise git:(main) ✗ git commit --amend [main 833283c] 测试commit --amend 是否能用  1 file changed, 2 insertions(+)  create mode 100644 rebase1.txt
 
先提交一次在gitexercise上。
1 2 3 4 原来文件内容: 先测试commit 撤销开始 修改后: 先测试commit 撤销开始,我修改了一下错误以amend
 
 
显而易见:
用 commit --amend 可以修复当前提交的错误。使用方式:
 
需要注意的有一点:commit --amend 并不是直接修改原 commit 的内容,而是生成一条新的 commit。
rebase 前置条件。
 
在test-rebase分支上
 
1 2 3 4 5 6 7 8 9 10 ➜  git-exercise git:(test-rebase) git rebase main Auto-merging rebase1.txt CONFLICT (content): Merge conflict in  rebase1.txt error: could not apply afeeb31... rebase分支提交 Resolve all conflicts manually, mark them as resolved with"git add/rm <conflicted_files>" , then  run "git rebase --continue" . You can instead skip this commit: run "git rebase --skip" . To abort and get back to the state before "git rebase" , run "git rebase --abort" . Could not apply afeeb31... rebase分支提交
 
 
打开文件可以看到冲突的地方已经标出来了。解决冲突之后。
1 2 3 4 5 6 ➜  git-exercise git:(cb9e2c9) ✗ git add rebase1.txt  ➜  git-exercise git:(cb9e2c9) ✗ git rebase --continue  [detached HEAD 5ce1e26] rebase分支提交  2 files changed, 2 insertions(+), 2 deletions(-)  create mode 100644 rebase2.txt Successfully rebased and updated refs/heads/test-rebase.
 
 
test-rebase分支直接 以 main分支最新提交为起点 再把自己分支上的提交依次排在main起点开始。
通过这样的方式,就让本来分叉了的提交历史重新回到了一条线。这种「重新设置基础点」的操作,就是 rebase 的含义。
另外,在 rebase 之后,记得切回 master 再 merge 一下,把 master 移到最新的 commit:
1 2 3 git checkout master git merge branch1
 
1 2 3 4 5 6 7 8 9 10 11 ➜  git-exercise git:(test-rebase) git checkout main Switched to branch 'main'  Your branch is ahead of 'origin/main'  by 3 commits.   (use "git push"  to publish your local  commits) ➜  git-exercise git:(main) git merge test-rebase  Updating cb9e2c9..5ce1e26 Fast-forward  rebase1.txt | 3 +--  rebase2.txt | 1 +  2 files changed, 2 insertions(+), 2 deletions(-)  create mode 100644 rebase2.txt
 
其实很好记。对于一般情况,总会操作master。那么
merge实际上都要从master出发使用,git merge other-branch 
rebase实际上都是:git rebase master意思就是以master为基点,变基。 
注意的是操作master的rebase,最后要在master上merge一下以达到最新。 
 
 
 
如果是merge而不是rebase,从开始测试rebase(包括这个commit开始一共4个因为会新产生一个,并产生分叉。
对比rebase和merge。
总结 rebase 指令,它可以改变 commit 序列的基础点。它的使用方式很简单:
 
需要说明的是,rebase 是站在需要被 rebase 的 commit 上进行操作,这点和 merge 是不同的。
下一篇:处理一些提交上的错误大全