Re: how to squash two commits into only one
- From
- Tor Arntsen <tor@spacetec.no>
- Date
- Mar 29, 2011, 08:50 UTC
- Message-ID
- <AANLkTikLuGh6VRReWvu4yT1v2VE_FXPBCx+9kXMX1Mni@mail.gmail.com>
- In-Reply-To
- <AANLkTin-0cBa293CQSf4kwMUG=w2j5zkSz1oBDhNqaNe@mail.gmail.com>
On Tue, Mar 29, 2011 at 10:24, Lynn Lin <lynn.xin.lin@gmail.com> wrote:
Show 6 quoted lines
> Thanks .However my case is like: > git log > 1 > 2 > > I want to squash 1 to 2,what's the commit I should specify with rebase -i
That would be 3, i.e. rebase -i 3
That won't work if there are only two commits in total.. rebase needs a commit before the one you want to squash to. But in that case you would do 'git reset HEAD^1' (and end up with only commit 2, and 'M' for the modifications originally done in commit 1), 'git add' <the files changed>, 'git commit --amend', which is exctly what Henrik described earlier.
There is a particular problem if you have this sequence: git log 1 2 3 4 5 (initial commit)
and you want to squash 4 into 5.. there's no hash 6 to rebase -i to. There are several ways to do it, but what I do is something like this: git checkout 4 git checkout -b tmp git reset HEAD^1 <add files> git commit --amend git merge master git branch -M tmp master (<---- rename the tmp branch to 'master')
the new master now has 4 and 5 squashed.
I'm sure others on this list have different ways to do this, but it's the one method that looked obvious to me so that's what I did when I needed it (anyway it's more or less how I fixed historic commits in the past before I learned about 'rebase -i' .. well, rebase -i didn't even exist with the versions of Git I used back then anyway).
-Tor