Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use git rebase to clean up a convoluted history

Tags:

git

merge

rebase

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.

like image 321
Lawrence I. Siden Avatar asked Jun 11 '10 21:06

Lawrence I. Siden


People also ask

Does git rebase remove history?

The history can be messed up using rebase, but normally remote repo will not accept the change that modifies the history (unless you use git push --force), but even more, the developer with push permission can delete the branch completely (git push origin :branch-name).

How does git rebase rewrite history?

Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.

Does rebase makes merge history messy?

Interactive rebasing gives you complete control over what your project history looks like. This affords a lot of freedom to developers, as it lets them commit a "messy" history while they're focused on writing code, then go back and clean it up after the fact.


1 Answers

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
like image 184
Wayne Conrad Avatar answered Oct 12 '22 23:10

Wayne Conrad