4

I have about 10 commits on my branch, let's say ID 1 through 10. I want to squash commits 4, 5 and 6 together, but leave the rest untouched. Every tutorial I found starts with the current HEAD and goes backwards with git rebase -i HEAD~(x amount). But how do I start the rebase at an earlier commit and specify a range such as git rebase -i 6...4 ?

Thank you

1
  • 1
    The tutorials are that way because they have to be. Rebase copies commits, leaving the originals untouched (it has to; Git can't change anything once committed). You therefore must re-copy all commits "after" the fixup ones, because the original uncopied commits point back to their earlier, original, un-fixed-up commits. See jared's answer below.
    – torek
    Commented Feb 20, 2017 at 19:38

2 Answers 2

5

Just do an interactive rebase, squash of fixup the commits you want squashed or fixed up.

git rebase -i HEAD~10

pick a4461d3 
pick d998164 
pick 0a1f6e1 
f 310ba9d 
f 60b7e01
f 7baef60
pick bb9a551
pick badbad1
pick fd9a10c
pick 59ed66f

# Rebase e7e1369..60b7e01 onto fd9a10c (10 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
1
  • Didn't realize I could do that. Thanks Commented Feb 22, 2017 at 1:32
1

Well, @jaredr has already answered your question. Coming to the second part of the question, GIT doesn't offers such commands (git rebase -i 4..6). and in fact it shouldn't! Imagine how much juggling GIT will have to perform if it starts offering such command (where it has to perform an operation on some commits and stack others. Merging properly adds additional complexity to it. Therefore, it transfers this job to the user itself as he/she is the one who knows the code better.) Moreover, it provides helping mechanisms like Git squash where you can temporarily dump your commits and work on something else.

1
  • I sorta get what you mean. However, in a completely linear commit history (assuming no merges happened), I still don't see how this would add complexity. After all, we could just assume 3, 4, 5 all happened at once, and then move on to 6 right? Commented Feb 22, 2017 at 1:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.