赞
踩
Git rebase 是版本控制系统 Git 中一个功能强大、使用广泛的命令。它用于将一个分支中的改动整合到另一个分支中。rebase与merge不同, merge会创建一个新的提交,而rebase则是将一系列提交移动或合并到一个新的基础提交中。下面是详细解释:
rebase的本质是将当前分支的提交重新应用到另一个基础提交上。这通常用于保持线性的项目历史,避免产生不必要的合并提交。
请看下面的例子,feature
分支基于main
分支的一个较早提交:
A---B---C---D main
\
E---F---G feature
如果您在feature
分支上运行 git rebase main
,Git 会:
B
)。E
、F
和 G
)以来,feature
中每次提交所引入的差异。feature
分支中暂时删除这些提交。main
分支中的提交应用到当前提交 (A-B-C-D
)。E
、F
和 G
提交引入的改动重新应用到 D
分支之上。重定向后,历史记录将如下所示:
A---B---C---D main
\
E'---F'---G' feature
注意 E
、F
和 G
已经被新的提交版本 E'
、F'
和 G'
取代。这些新提交的改动相同,但提交哈希值不同,因为它们现在基于 D
。
交互式重置: git rebase -i <base-branch>
。
常规重置: git rebase <base-branch>
重置:
合并:
###工作流程示例
git checkout feature
git rebase main
git add <file
git rebase --continue
或终止
git rebase --abort
git push origin feature --force
总之,"git rebase "是简化和清理提交历史的工具,但在使用时应了解它对项目提交历史的影响以及团队内部的协调。
Git rebase is a powerful and widely used command in Git, a version control system. It is used to integrate changes from one branch into another. Unlike merging, which creates a new commit for the merge, rebasing moves or combines a sequence of commits to a new base commit. Here’s a detailed explanation:
Rebasing essentially re-applies commits from your current branch onto another base commit. This is often used to maintain a linear project history, avoiding the creation of unnecessary merge commits.
Consider the following example where feature
branch is based on an older commit of the main
branch:
A---B---C---D main
\
E---F---G feature
If you run git rebase main
while on the feature
branch, Git will:
B
in this case).feature
since the common ancestor (E
, F
, and G
).feature
branch.main
branch up to the current commit (A-B-C-D
).E
, F
, and G
on top of D
.After rebasing, the history would look like this:
A---B---C---D main
\
E'---F'---G' feature
Notice that E
, F
, and G
have been replaced with new commits E'
, F'
, and G'
. These new commits have the same changes but different commit hashes because they are now based on D
.
Interactive Rebase: git rebase -i <base-branch>
Regular Rebase: git rebase <base-branch>
Rebase:
Merge:
git checkout feature
git rebase main
git add <file>
git rebase --continue
or to abort:
git rebase --abort
git push origin feature --force
In summary, git rebase
is a tool for streamlining and cleaning up your commit history, but it should be used with an understanding of its effects on your project’s commit history and coordination within your team.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。