Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase branch onto older commit

Tags:

git

git-rebase

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?

like image 347
Mr Redstoner Avatar asked Oct 10 '19 09:10

Mr Redstoner


People also ask

What does git rebase onto do?

What is git rebase? From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base.

Does rebase rewrite commit 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.


2 Answers

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.

like image 74
Rozart Avatar answered Oct 09 '22 20:10

Rozart


git rebase --onto start work^ issue-fix

It tries to move the commits from work(included) to the tip of issue-fix onto the new base start.

If you want to skip or squash some of the commits, use the interactive mode with -i. The interactive page will show you how to.

If you want to keep merge commits, you could add -p. To keep empty commits, use --keep-empty.

You may have some other needs not mentioned in the question. But most probably you could find proper options in the manual of git rebase.

like image 45
ElpieKay Avatar answered Oct 09 '22 19:10

ElpieKay