git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: how to squash two commits into only one

From
TATor 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
Previous: Lynn LinNext: Alex Riesen
Message 4 of 14 in “how to squash two commits into only one”
  1. Lynn LinMar 29, 2011
  2. Tor ArntsenMar 29, 2011
  3. Lynn LinMar 29, 2011
  4. Tor ArntsenMar 29, 2011
  5. Alex RiesenMar 29, 2011
  6. Lynn LinMar 29, 2011
  7. Alex RiesenMar 29, 2011
  8. Joshua JuranMar 29, 2011
  9. Nguyen Thai Ngoc DuyMar 29, 2011
  10. Joshua JuranMar 29, 2011
  11. Will PalmerMar 29, 2011
  12. Junio C HamanoMar 29, 2011
  13. Dirk SüsserottMar 30, 2011
  14. Lynn LinMar 29, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.