Search code examples
gitgit-rebase

Git rebase branch onto older commit


Note: How this situation came to be is more of a tale that anything and not really relevant IMO.

Given the following git commit graph, starting from oldest, all pushed to origin

          start
            |
         breaking
       /          \
fix #0             work
    |               |
fix #1(master)     morework(issue-fix)

I need to move my branch (issue-fix) to be based on start, rather than breaking, that is turn it into

          start
       /         \
breaking            |
    |               |
fix #0             work
    |               |
fix #1(master)     morework(issue-fix)

The changes in breaking are unrelated to the issue fix (different part of project), hovever they cause the CI to fail for that branch, as the testing covers that part of the project as well.

I assumed a rebase would work, but after checking out morework, attempting it in SourceTree did nothing, manually running git rebase start just prints Current branch issue-fix is up to date

Is there a clean and effiecient way of doing this? I don't want to make a new branch and move everything over manually if I don't have to.

EDIT: Also, issue-fix has been pulled into another branch, thereby introducing the wrong commits there as well. Will the same process work there, or will doing it on issue-fix break that branch?


Solution

  • You should use interactive rebase to get rid of the breaking commit from your issue-fix branch.

    In your case you should, checkout to the issue-fix branch and do:

    git rebase -i HEAD~3

    Then when the editor is open you should be able to choose which commits you want to leave and which you want to get rid of:

    drop 2dsafa ... will delete the commit you want to get rid of
    pick sdfsadf .. will leave this commit
    pick dfsa4sdc .. will leave this commit
    

    If your issue-fix branch have been already merged into other branches in its broken state, then things become a bit more complicated.

    In such case I would suggest to use the interactive rebase to get rid of the issue-fix commits from those other branches. After that I would rebase the commits from those other branches on top of issue-fix.