原理
使用方法
git rebase -i master
- 调整顺序

- 合并

- 丢弃提交
应用
- 整合提交历史

例子,修改历史提交信息,修改dit-leetcode为dir-leetcode
demonfox@ubuntu:~/linux-cpp$ git logcommit de88d5a96305a0424734f92278156d9dfd8ab551Author: 9DemonFox <1196752479@qq.com>Date: Mon May 23 08:11:49 2022 -0700-dit-leetcode 增加二叉树相关,以及leetcode1302commit d720a23e9629347b2f249ec70f984e3f16a68f76Author: DemonFox <1196752479@qq.com>Date: Fri May 20 13:34:07 2022 +0800-dir-leetcode small changescommit a4f6b9b8d2b29c6a73956d882bd004f99b3bffdeAuthor: 9DemonFox <1196752479@qq.com>Date: Thu May 19 22:24:34 2022 -0700增加leetcode文件夹用于练习算法commit 811cd8b86a9a4bab248dbbb41476adbe459e1454Author: 9DemonFox <1196752479@qq.com>Date: Sun Mar 27 22:54:35 2022 -0700修改oj支持多副本demonfox@ubuntu:~/linux-cpp$ git rebase -i HEAD~2Cannot rebase: Your index contains uncommitted changes.Please commit or stash them.demonfox@ubuntu:~/linux-cpp$ git commit -m "暂存"[master eb85efa] 暂存20 files changed, 509 insertions(+), 1 deletion(-)create mode 100644 .clang-formatcreate mode 100644 leetcode/101Template/main.cppcreate mode 100644 leetcode/4TreeSearch/574/copy.txtcreate mode 100755 leetcode/4TreeSearch/574/excutecreate mode 100644 leetcode/4TreeSearch/574/inputs/1.txtcreate mode 100644 leetcode/4TreeSearch/574/inputs/2.txtcreate mode 100644 leetcode/4TreeSearch/574/makefilecreate mode 100644 leetcode/4TreeSearch/574/test.cppcreate mode 100644 leetcode/4TreeSearch/993/copy.txtcreate mode 100755 leetcode/4TreeSearch/993/excutecreate mode 100644 leetcode/4TreeSearch/993/inputs/1.txtcreate mode 100644 leetcode/4TreeSearch/993/inputs/2.txtcreate mode 100644 leetcode/4TreeSearch/993/inputs/3.txtcreate mode 100644 leetcode/4TreeSearch/993/inputs/4.txtcreate mode 100644 leetcode/4TreeSearch/993/makefilecreate mode 100644 leetcode/4TreeSearch/993/test.cppcreate mode 100644 tricks/bitmap/main.cppdemonfox@ubuntu:~/linux-cpp$ git rebase -i HEAD~2Successfully rebased and updated refs/heads/master.demonfox@ubuntu:~/linux-cpp$ git rebase -i HEAD~2Stopped at de88d5a96305a0424734f92278156d9dfd8ab551... -dit-leetcode 增加二叉树相关,以及leetcode1302You can amend the commit now, withgit commit --amendOnce you are satisfied with your changes, rungit rebase --continuedemonfox@ubuntu:~/linux-cpp$ git commit --amend[detached HEAD 1c7b9d3] -dir-leetcode 增加二叉树相关,以及leetcode1302Date: Mon May 23 08:11:49 2022 -070010 files changed, 220 insertions(+), 1 deletion(-)create mode 100644 leetcode/4TreeSearch/1302/copy.txtcreate mode 100755 leetcode/4TreeSearch/1302/excutecreate mode 100644 leetcode/4TreeSearch/1302/inputs/1.txtcreate mode 100644 leetcode/4TreeSearch/1302/makefilecreate mode 100644 leetcode/4TreeSearch/1302/test.cppcreate mode 100644 leetcode/tools/bitree.cppcreate mode 100644 leetcode/tools/bitree.hcreate mode 100644 leetcode/tools/makefilecreate mode 100644 leetcode/tools/utest_bitree.cppdemonfox@ubuntu:~/linux-cpp$ git rebase --continueSuccessfully rebased and updated refs/heads/master.demonfox@ubuntu:~/linux-cpp$ git logcommit f7901179dec12c478e55b4563eebe07ba5816487Author: 9DemonFox <1196752479@qq.com>Date: Mon May 30 09:44:40 2022 -0700暂存commit 1c7b9d36eb73e3a23e1ba36273394627a90a9646Author: 9DemonFox <1196752479@qq.com>Date: Mon May 23 08:11:49 2022 -0700-dir-leetcode 增加二叉树相关,以及leetcode1302commit d720a23e9629347b2f249ec70f984e3f16a68f76Author: DemonFox <1196752479@qq.com>Date: Fri May 20 13:34:07 2022 +0800-dir-leetcode small changescommit a4f6b9b8d2b29c6a73956d882bd004f99b3bffdeAuthor: 9DemonFox <1196752479@qq.com>Date: Thu May 19 22:24:34 2022 -0700增加leetcode文件夹用于练习算法:...skipping...commit f7901179dec12c478e55b4563eebe07ba5816487Author: 9DemonFox <1196752479@qq.com>Date: Mon May 30 09:44:40 2022 -0700暂存commit 1c7b9d36eb73e3a23e1ba36273394627a90a9646Author: 9DemonFox <1196752479@qq.com>Date: Mon May 23 08:11:49 2022 -0700-dir-leetcode 增加二叉树相关,以及leetcode1302commit d720a23e9629347b2f249ec70f984e3f16a68f76Author: DemonFox <1196752479@qq.com>Date: Fri May 20 13:34:07 2022 +0800-dir-leetcode small changescommit a4f6b9b8d2b29c6a73956d882bd004f99b3bffdeAuthor: 9DemonFox <1196752479@qq.com>Date: Thu May 19 22:24:34 2022 -0700增加leetcode文件夹用于练习算法commit 811cd8b86a9a4bab248dbbb41476adbe459e1454Author: 9DemonFox <1196752479@qq.com>Date: Sun Mar 27 22:54:35 2022 -0700修改oj支持多副本commit 45b5f505488194edd413ce799425bf84ee270eb8Author: 9DemonFox <1196752479@qq.com>Date: Sun Mar 27 10:59:40 2022 -0700增加oj,方便练习题目commit 039b2b1cc736a9ccc4a205444914a5419def6345Author: 9DemonFox <1196752479@qq.com>demonfox@ubuntu:~/linux-cpp$
整理分支后通常需要使用git push -f来覆盖远程分支
有两种情况下适合使用这个命令:
确定需要覆覆盖提交,就像上面的那种情况,在明确部分提交会导致异常时,可以使用新的提交去覆盖。
需要整理历史提交记录时,有时候项目的 Commit Logs 可能比较乱,不能清晰的看出每一次提交的作用,可以使用 rebase 命令来清理历史提交记录。因为改变了历史,所以正常来说是 push不成功的,所以需要使用 force push来解决这个问题。
