如何使用 git rebase 来清理错综复杂的历史
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3026218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use git rebase to clean up a convoluted history
提问by Lawrence I. Siden
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:
在我的笔记本电脑和工作场所以及家里的台式机上与六个不同的分支和合并一起工作了几个星期之后,我的历史变得有点令人费解。例如,我只是做了一个 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 ....
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!
我想用 git rebase 来清理它。为此,我创建了一个新分支 rebase-master,并在这个分支上尝试了 git rebase <common-ancestor>。但是,我必须解决很多冲突,最终结果在分支 rebase-master 上不再匹配 master 上的相应版本,该版本已经过测试并有效!
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.
我是这个项目的唯一开发者,所以没有其他人会受到影响。
回答by Wayne Conrad
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.
当需要将分支集成到 master 时,不要合并它。相反,将此分支重新设置为针对主分支。这将使分支不再看起来像一个分支,而只是在树的顶部有更多的增长。您可以在变基期间解决任何合并冲突。
$ git fetch origin
$ git rebase origin/master
Now, merge the branch into master. This will be a fast-forward merge.
现在,将分支合并到 master 中。这将是一个快进合并。
$ git checkout master
$ git merge foobranch
And now push the work upstream.
现在将工作推向上游。
$ git push
回答by VonC
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:
对于可以强制推送分支的存储库(用 rebase 在本地创建的新历史记录替换远程历史记录)的正常过程是:
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.
从 rebase 会话中,您可以修剪 Git 提交和压缩历史记录,以获得您需要的那种历史记录。