Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive rebase of a branch with its point of divergence from master

Tags:

git

rebase

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.

like image 491
Aaron Avatar asked May 02 '12 14:05

Aaron


People also ask

What is interactive rebase?

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.

How do I fix the divergent branch in git?

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.


2 Answers

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.

like image 138
twalberg Avatar answered Sep 20 '22 14:09

twalberg


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` 
like image 20
Akrikos Avatar answered Sep 18 '22 14:09

Akrikos