Let's say I have a topic branch whose entire history I want to rewrite since it was originally created from master for a pull request. For whatever reason, it is not easy or obvious using git log
to determine the commit hash I want to pass to
git rebase -i <commit>
I know I can use git merge-base <branch1> <branch2 || master>
to find the commit that two references can trace their ancestry from and can use that to determine the commit. What I would like to know is if there is a better way to interactively rebase this whole branch (whether master has advanced or not) than using
git rebase -i `git merge-base my_branch master`
EDIT: I do not want to change the parent of the first commit made on this branch so git rebase -i master
would only work in the case where both master has not advanced since the branch was created and the branch was created from the commit master currently points to.
Interactive rebase in Git is a tool that provides more manual control of your history revision process. When using interactive rebase, you will specify a point on your branch's history, and then you will be presented with a list of commits up until that point.
This can be properly resolved by committing changes from the merge first by git commit , then adding and committing the unstaged changes as usual (e.g. by git commit -a ). Show activity on this post. Replace 123 with number of commits your branch has diverged from origin.
Maybe I'm misunderstanding your question, but I think git rebase -i master
should do what you want. It will figure out the merge base and rebase the entire branch from that point to the current HEAD so that it appears to have been branched from the current tip of master.
Also, if master has not advanced, then rebasing will pretty much be a no-op.
I only found one iterative improvement to the command you listed. Your command:
git rebase -i `git merge-base my_branch master`
An improvement would be to not have to remember the name of the branch you want to rebase. Assuming you're currently on the branch you want to rebase (which is almost always the case in my experience), you could use:
git rebase -i `git merge-base HEAD master`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With