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:
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:
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:
Of course, I staged and committed this change:
git add code.py
git commit -m "Commit 19"
Now, consider the history:
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:
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.
After following your advice and running the rebase
command (thanks! π) I get the following screen:
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:
Now when I get prompted for a commit message, I can provide the message "Commit 17+19":
And now, see our beautiful history:
Thanks again! ππ»