git 更改某个提交内容和将当前改动追加到某次commit 上

尤其适用于gerrit 上的CL 未submit 之前,有多个patch时的多个功能点并行开发。

rebase 前,可以创建一个分支,用于rebase:
git checkout -b rebase_tmp

直接更改某次提交(改动某个指定的commit)

  1. 将HEAD移到需要更改的commit上:
    git rebase -i 0bdf89^
    找到需要更改的commit, 将行首的pick改成edit, 按esc, 输入:wq退出
    (弹出的交互式界面中显示的commit 信息,与git log 显示的顺序相反,即父节点在上显示)
  2. 更改文件
  3. 使用git add 改动的文件添加改动文件到暂存
  4. 使用git commit --amend追加改动到第一步中指定的commit上
  5. 使用git rebase --continue移动HEAD到最新的commit处
  6. 解决冲突:
    • 编辑冲突文件, 解决冲突
    • git add .
    • git commit --amend
  7. 解决冲突之后再执行git rebase --continue

将工作空间中的改动追加到某次提交上

**note:**第0步和第2步(加粗)与上面步骤不同,其余步骤相同

  1. 保存工作空间中的改动git stash
  2. 将HEAD移到需要更改的commit上:
    git rebase f744c32^ --interactive
    找到需要更改的commit, 将行首的pick改成edit, 按esc, 输入:wq退出
  3. 执行命令git stash pop
  4. 使用git add 改动的文件添加改动文件到暂存
  5. 使用git commit --amend追加改动到第一步中指定的commit上
  6. 使用git rebase --continue移动HEAD到最新的commit处
  7. 解决冲突:
    • 编辑冲突文件, 解决冲突
    • git add .
    • git commit --amend
  8. 解决冲突之后再执行git rebase --continue

这样处理的优点

如果branchB分支需要branchA分支上的某个功能, 只需要找到这个功能的惟一的一个提交记录即可, 就不需要在很多commit之中寻找这个功能点的相关提交记录. 更改合并之后再移动功能点, 就简单了许多, 执行找到功能点的惟一一个提交记录, 让后使用git cherry-pick commit-hash即可,

rebase失败如何恢复

使用reflog撤销变基

$ git reflog
8c79a588 HEAD@{0}: checkout: moving from JM-5995 to JM-5997
e7b1f794 HEAD@{1}: checkout: moving from JM-5876 to JM-5995
97c945d8 HEAD@{2}: commit: fix response
81888803 HEAD@{3}: commit: sql的整理

上面是所有的日志 我们可以逐条分析,找到我们rebase的节点 只要输入下面的命令就好了

git reset --hard HEAD@{3}
或者
git reset --hard 81888803

rebase -i 说明

修改更早的提交或修改多个提交就需要用到…git rebase -i parentCommitID,其机理是通过重新衍合parentCommitID之后的全部提交,所以该操作会改变parentCommitID结点之后所有提交的commit id。

通过rebase -i我们可以交互式的运行rebase,以达到修改更早的提交或修改多个提交.

弹出的交互式窗口中的信息

文件名:.git/rebase-merge/git-rebase-todo

1   pick 5f26aa0 JiraID:BMAP-125:feat(bus_interface): add uart interface   
  1 pick 14246c2 JiraID:BMAP-126:feat(PHM): add health monitor interface   
  2                                                                        
  3 # Rebase f3f2b0d..14246c2 onto f3f2b0d (2 commands)                    
  4 #                                                                      
  5 # Commands:                                                            
  6 # p, pick = use commit                                                 
  7 # r, reword = use commit, but edit the commit message                  
  8 # e, edit = use commit, but stop for amending                          
  9 # s, squash = use commit, but meld into previous commit                
 10 # f, fixup = like "squash", but discard this commit's log message      
 11 # x, exec = run command (the rest of the line) using shell             
 12 # d, drop = remove commit                                              
 13 #                                                                      
 14 # These lines can be re-ordered; they are executed from top to bottom. 
 15 #                                                                      
 16 # If you remove a line here THAT COMMIT WILL BE LOST.                  
 17 #                                                                      
 18 # However, if you remove everything, the rebase will be aborted.       
 19 #                                                                      
 20 # Note that empty commits are commented out                            

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐