
How to rebase on a single branch
How to rebase on a single branch 관련
You can also use git rebase
while looking at a history of a single branch.
Let's see if you can help me here.
Say I worked from feature_branch_2
, and specifically edited the file code.py
. I started by changing all strings to be wrapped by double quotes rather than single quotes:

'
into "
in code.py
<Source: Brief>
Then, I staged and committed:
git add code.py
git commit -m "Commit 17"
I then decided to add a new function at the beginning of the file:

another_feature
<Source: Brief>
Again, I staged and committed:
git add code.py
git commit -m "Commit 18"
And now I realized I actually forgot to change the single quotes to double quotes wrapping the __main__
(as you might have noticed), so I did that too:

'__main__'
into "__main__"
<Source: Brief>
Of course, I staged and committed this change:
git add code.py
git commit -m "Commit 19"
Now, consider the history:

<Source: Brief>
It isn't really nice, is it? I mean, I have two commits that are related to one another, "Commit 17" and "Commit 19" (turning '
s into "
s), but they are split by the unrelated "Commit 18" (where I added a new function). What can we do? 🤔 Can you help me?
Intuitively, I want to edit the history here:

<Source: Brief>
So, what would you do?
You are right! 👏🏻
I can rebase the history from "Commit 17" to "Commit 19", on top of "Commit 15". To do that:
git rebase --interactive --onto <SHA_OF_COMMIT_15> <SHA_OF_COMMIT_15>
Notice I specified "Commit 15" as the beginning of the range of commits, excluding this commit. And I didn't need to explicitly specify HEAD
as the last parameter.

rebase --onto
on a single branch<Source: Brief>
After following your advice and running the rebase
command (thanks! 😇) I get the following screen:

<Source: Brief>
So what would I do? I want to put "Commit 19" before "Commit 18", so it comes right after "Commit 17". I can go further and squash them together, like so:

<Source: Brief>
Now when I get prompted for a commit message, I can provide the message "Commit 17+19":

<Source: Brief>
And now, see our beautiful history:

<Source: Brief>
Thanks again! 🙌🏻