Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

merge - how to use git rebase to clean up a convoluted history

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...