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

Re: how to squash two commits into only one

From
DSDirk Süsserott <newsletter@dirk.my1.cc>
Date
Mar 30, 2011, 19:22 UTC
Message-ID
<4D9382ED.4090806@dirk.my1.cc>
In-Reply-To
<7v1v1psrvr.fsf@alter.siamese.dyndns.org>
Am 29.03.2011 20:28 schrieb Junio C Hamano:
Show 11 quoted lines
> Nguyen Thai Ngoc Duy<pclouds@gmail.com>  writes:
>
>>> Actually, that should be: `git reset --soft HEAD^; git commit --amend`.
>>
>> "git rebase --root" does not seem a bad idea though.
>
> The lack of this did't annoy me enough (one woudln't have to deal with
> root commits too often) so I wouldn't bother implementing it myself, but
> "git rebase --root" especially with "-i" would be a nice addition.
>
> i.e. well written and explained patches welcome.

Lynn, this won't help you right now, but perhaps in the future: When I create a new repo, the first thing I do is creating an empty commit, like so:

git init my_repo cd my_repo git commit --allow-empty -m "Initial commit (empty)"

This creates an empty root commit which is helpful not only in your case.

Another way to solve your problem would be to prepend that empty root commit to your repo afterwards. It's a bit weird, but it works -- at least if you don't have branches (except for "master").

git checkout --orphan new_master (1) git rm -rf . (2) git commit --allow-empty -m "Initial commit (empty)" (3) git rebase new_master master (4) git branch -d new_master (5)

(1) creates a new branch 'new_master' which is totally independent
     of any other branch in the repo, thus the option's name. But it
     silently does a "git add .", i.e. it stages all files that were
     checked out before issuing the command. See 'checkout' docs
     for details.
(2) removes all the files staged in (1), so that you have a new
     starting point which is really clean.
(3) creates the empty root commit on 'new_master'
(4) replays all changes of 'master' to 'new_master'. 'new_master'
     then has three commits: the root commit and the two commits from
     'master'
(5) removes the 'new_master' which is no longer needed.

And NOW you can do your 'rebase -i <root_commit>' to squash your actual commits.

If you have more than one branch you would have to repeat step (4) with the other branches.

HTH, Dirk

Previous: Junio C HamanoNext: Lynn Lin
Message 13 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.