如何使用 git rebase 清理复杂的历史记录

发布于 2024-09-05 07:31:09 字数 946 浏览 7 评论 0原文

在我的笔记本电脑、工作场所和家里的台式机上与六个不同的分支和合并工作了几周后,我的历史变得有点复杂。例如,我只是做了一次 fetch,然后将 master 与 origin/master 合并。现在,当我执行 git show-branches 时,输出如下所示:

! [login] Changed domain name.
 ! [master] Merge remote branch 'origin/master'
  ! [migrate-1.9] Migrating to 1.9.1 on Heroku
   ! [rebase-master] Merge remote branch 'origin/master'
----
 - - [master] Merge remote branch 'origin/master'
 + + [master^2] A bit of re-arranging and cleanup.
 - - [master^2^] Merge branch 'rpx-login'
 + + [master^2^^2] Commented out some debug logging.
 + + [master^2^^2^] Monkey-patched Rack::Request#ip
 + + [master^2^^2~2] dump each request to log
....

我想用 git rebase 来清理它。为此,我创建了一个新分支 rebase-master,并在这个分支上尝试了 git rebase。但是,我必须解决许多冲突,并且分支 rebase-master 上的最终结果不再与 master 上的相应版本匹配,该版本已经经过测试并且可以工作!

我以为我在某个地方看到了这个问题的解决方案,但现在找不到了。有谁知道该怎么做?或者,当我开始删除已经合并的不需要的分支时,这些复杂的引用名称会消失吗?

我是这个项目的唯一开发者,所以没有其他人会受到影响。

After working for several weeks with a half dozen different branches and merges, on both my laptop and work and my desktop at home, my history has gotten a bit convoluted. For example, I just did a fetch, then merged master with origin/master. Now, when I do git show-branches, the output looks like this:

! [login] Changed domain name.
 ! [master] Merge remote branch 'origin/master'
  ! [migrate-1.9] Migrating to 1.9.1 on Heroku
   ! [rebase-master] Merge remote branch 'origin/master'
----
 - - [master] Merge remote branch 'origin/master'
 + + [master^2] A bit of re-arranging and cleanup.
 - - [master^2^] Merge branch 'rpx-login'
 + + [master^2^^2] Commented out some debug logging.
 + + [master^2^^2^] Monkey-patched Rack::Request#ip
 + + [master^2^^2~2] dump each request to log
....

I would like to clean this up with a git rebase. I created a new branch, rebase-master, for this purpose, and on this branch tried git rebase <common-ancestor>. However, I have to resolve many conflicts, and the end result on branch rebase-master no longer matches the corresponding version on master, which has already been tested and works!

I thought I saw a solution to this somewhere but can't find it anymore. Does anyone know how to do this? Or will these convoluted ref names go away when I start deleting un-needed branches that I have already merged with?

I am the sole developer on this project, so there is no one else who will be affected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-09-12 07:31:10

对于可以强制推送分支的存储库(将远程历史记录替换为通过变基在本地创建的新分支),正常过程是:

git rebase --interactive

但同样,只有当您是唯一从存储库拉取的人时,这才有效,即使如此,您也必须将一些本地分支重新初始化为已重写的新远程跟踪分支。

在变基会话中,您可以修剪 Git 提交和压缩历史记录 ,以获得您需要的历史记录。

The normal process, for repos where you can force push a branch (replacing the remote history by a new one created locally by a rebase), is to do:

git rebase --interactive

But again, that is only valid if you are the only one pulling from your repos, and even then you will have to reinitialize some of your local branches to the new remote tracking ones that have been rewritten.

From rebase session, you can trimming Git commits and squash history, in order to get the kind of history you need.

血之狂魔 2024-09-12 07:31:09

清理错综复杂的历史的最好方法是保持历史的线性。您可以通过避免快进以外的任何类型的合并来做到这一点。

工作流程是这样的。

$ git checkout -b foobranch
<do stuff>
$ git commit
<do stuff>
$ git commit
...

当需要将分支集成到 master 时,不要合并它。相反,根据主分支重新设置此分支的基础。这将使树枝看起来不再像树枝,而只是在树顶上更多地生长。您可以在变基期间解决任何合并冲突。

$ git fetch origin
$ git rebase origin/master

现在,将分支合并到 master 中。这将是一次快进合并。

$ git checkout master
$ git merge foobranch

现在将工作推向上游。

$ git push

The best way to clean up a convoluted history is to keep the history linear. You do that by avoiding any kind of merge other than fast-forward.

The work flow goes like this.

$ git checkout -b foobranch
<do stuff>
$ git commit
<do stuff>
$ git commit
...

When it's time to integrate the branch into master, don't merge it. Instead, rebase this branch against the master. That will make the branch no longer look like a branch, but but simply more growth on the top of the tree. You resolve any merge conflicts during the rebase.

$ git fetch origin
$ git rebase origin/master

Now, merge the branch into master. This will be a fast-forward merge.

$ git checkout master
$ git merge foobranch

And now push the work upstream.

$ git push
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文