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

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

问题描述

在工作几个星期后,我的笔记本电脑和工作以及家中的桌面上都有六个不同的分支机构合并,我的历史变得有些复杂。例如,我刚刚进行了一次抓取,然后将原生与主人合并。现在,当我做git show-branches时,输出如下所示:

 
! [登录]更改了域名。
! [master]合并远程分支'origin / master'
! [migrate-1.9]在Heroku上迁移到1.9.1
! [rebase-master]合并远程分支'origin / master'
----
- - [master]合并远程分支'origin / master'
++ [master ^ 2] A重新安排和清理的一点。
- [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上的相应版本匹配!



我以为我在某处看到了解决方案,但找不到它了。有谁知道如何做到这一点?或者当我开始删除我已经合并的不需要的分支时,这些令人费解的ref名字会消失?



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

解决方案

清理复杂历史的最佳方法是保持历史线性。您可以通过避免任何合并而不是快进。



工作流程如下所示。

  $ git checkout -b foobranch 
$ git commit
$ git commit
...

当需要集成分支时成为主人,不要合并它。相反,将这个分支与主人分开。这将使分支不再看起来像一个分支,但只是树上的更多增长。

  $ git fetch origin 
$ git rebase origin / master $ b $您可以解决rebase期间的任何合并冲突。 b

现在,将分支合并到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天全站免登陆