如何使用 git rebase 来清理错综复杂的历史 [英] how to use git rebase to clean up a convoluted history

查看:38
本文介绍了如何使用 git rebase 来清理错综复杂的历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<前>![登录] 更改了域名.![master] 合并远程分支 'origin/master'![migrate-1.9] 在 Heroku 上迁移到 1.9.1![rebase-master] 合并远程分支 'origin/master'----- - [master] 合并远程分支 'origin/master'+ + [master^2] 一些重新安排和清理.- - [master^2^] 合并分支 'rpx-login'+ + [master^2^^2] 注释掉了一些调试日志.+ + [master^2^^2^] Monkey-patched Rack::Request#ip+ + [master^2^^2~2] 转储每个请求到日志....

我想用 git rebase 来清理它.为此,我创建了一个新分支 rebase-master,并在这个分支上尝试了 git rebase <common-ancestor>.但是,我必须解决很多冲突,最终结果在分支 rebase-master 上不再匹配 master 上的相应版本,该版本已经过测试并有效!

我以为我在某处看到了解决方案,但再也找不到了.有谁知道如何做到这一点?或者当我开始删除已经合并的不需要的分支时,这些复杂的引用名称会消失吗?

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

解决方案

清理复杂历史的最佳方法是保持历史线性.您可以通过避免除快进之外的任何类型的合并来实现这一点.

工作流程是这样的.

$ git checkout -b foobranch<做事>$ git提交<做事>$ git提交...

当需要将分支集成到 master 时,不要合并它.相反,将此分支重新设置为针对主分支.这将使分支不再看起来像一个分支,而只是在树的顶部有更多的增长.在变基期间解决所有合并冲突.

$ git fetch 来源$ git rebase origin/master

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

$ git checkout master$ git 合并 foobranch

现在将工作推向上游.

$ git push

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.

解决方案

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

这篇关于如何使用 git rebase 来清理错综复杂的历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆