Search code examples
gitgit-rebase

git: how to re-order (severely) a commit sequence


I'm building a retrospective project history from zip snapshots. I have already built a long branch sequence of commits from snapshots that were to hand. I have now added at the end some more 'found' snapshots that should be at various places 'in the middle' of the commit sequence.

I'm trying to use the git rebase -i <startCommit> to re-order the git snapshots. I simply swap around the pick list order. This should be simply a case of re-writing the commit objects, but keeping the underlying trees the same (because the snapshots haven't changed).

It looks like rebase, in this case, is still trying to create patches and having lots of conflicts, rather than doing the simple re-arrangement. Is there a more appropriate command for this particular case? I have no merges, and only the one branch. The whole repo is still very local and private.


Solution

  • don't bother with rebase. Make a new branch. If X is sha1 or treeish of commit to use, then:

    git checkout X -- .
    git add -A
    git commit -C X
    

    Repeat for all commits in the chronological order that you want.

    An example where we reverse the order of the last 5 commits:

    git checkout -b temp old_branch~5
    git checkout old_branch -- .
    git add -A
    git commit -C old_branch
    git checkout old_branch~1 -- .
    git add -A
    git commit -C old_branch~1
    git checkout old_branch~2 -- .
    git add -A
    git commit -C old_branch~2
    git checkout old_branch~3 -- .
    git add -A
    git commit -C old_branch~3
    git checkout old_branch~4 -- .
    git add -A
    git commit -C old_branch~4
    
    git push . HEAD:old_branch -f
    git checkout old_branch
    git branch -d temp
    
    git log -5 # to see the new history
    

    hope this helps.