2

I understand how to rebase using git rebase -i HEAD~X where X is the number of previous commits you want to reference.

But say you were working on a branch and you make a commit. You then switch to a different branch and do some work there. You keep doing this back and for a couple days, let's say.

Eventually if you want to squash one of your branches you won't be able to do that because doing something like HEAD~3 will reference commits from different branches.

I guess my question is really: how do I squash all the commits on a branch without squashing unrelated commits into it as well?

Uri
  • 2,306
  • 2
  • 24
  • 25

1 Answers1

3

I get the feeling you believe HEAD~3 means three commits back in the same order (wall clock time order) you made them. That's not what it means, it means the 3rd generation ancestor of current HEAD, following only the first parents.

It doesn't matter at all how you switch between branches.

Kalle Pokki
  • 4,939
  • 2
  • 17
  • 18
  • 1
    The way to say "the commit `HEAD` was at three moves ago" is `HEAD@{3}`. – Borealid Jan 25 '13 at 19:15
  • That's exactly what I thought it was. I will look into this more and if it turns out to be what I was missing I will mark this as the correct answer. – Uri Jan 25 '13 at 19:44
  • Branches are almost always easiest to visualize when you fire up `gitk --all`. There you see all the branches you have, you can find where your current HEAD is (tip of currently checked out branch), and easily count down three generations of commits from that HEAD following the first parents. Or a straight line, if you didn't make any merges. – Kalle Pokki Jan 25 '13 at 20:15
  • BTW `man git-rev-parse` has a very extensive list of all the possible ways to specify revisions in git. – Kalle Pokki Jan 25 '13 at 20:20