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

Re: Any way to "flatten" a series of changes in git

From
Jakub Narebski <jnareb@gmail.com>
Date
Oct 22, 2009, 21:57 UTC
Message-ID
<200910222357.15189.jnareb@gmail.com>
In-Reply-To
<26ae428a0910221411l73aa7cbak5c060925ccdf4cea@mail.gmail.com>
On Thu, 22 Oct 2009, Howard Miller wrote:
Show 10 quoted lines
> > You can use either "git merge --squash" or "git rebase --interactive"
> > (changing 'pick' to 'squash').
> >
> 
> Actually thinking some more.... I don't understand something about
> this. I don't actually want to merge or rebase with anything. I just
> want to say "make those commits a series of commits on a branch into
> just one commit with a new message". I seriously suspect I'm missing
> the point somewhere but what has that got to do with merging or
> rebasing?

Actually using "git merge --squash" is a bit different from using "git rebase --interactive".

1. "git merge --squash"
>From documentation:
  --squash::
        Produce the working tree and index state as if a real
        merge happened (except for the merge information),
        but do not actually make a commit or
        move the `HEAD`, nor record `$GIT_DIR/MERGE_HEAD` to
        cause the next `git commit` command to create a merge
        commit.  This allows you to create a single commit on
        top of the current branch whose effect is the same as
        merging another branch (or more in case of an octopus).

This means for example if you did your changes on a separate topic branch, and you want to merge your changes into 'master' branch, you would do

  $ git checkout master
  $ git merge side-branch
which would result in the following history:
   ---*---*---*---*---*---*---*---M         <-- master
       \                         /
        \-.---.---.---.---.---.-/           <-- side-branch

If you used '--squash' option to git-merge, because changes were made in throwaway topic branch, and as you said only final result matter, you would get:

  $ git checkout master
  $ git merge --squash side-branch
   ---*---*---*---*---*---*---*---M'        <-- master
       \                         
        \-.---.---.---.---.---.             <-- side-branch

where commit M' has the same contents (the same tree) as commit M in previous example, but is not a merge commit.

If you simply want to squash last say 5 commits into one, you can use "git merge --squash" for it in the following way:

  $ git reset --hard HEAD~5
  $ git merge --squash --no-ff HEAD@{1}

which means basically: rewind to state 5 commits back, then merge in changes from before rewind, squashing them. The --no-ff is needed because otherwise it would be fast-forward and no commit would be created.

2. "git rebase --interactive"

The interactive rebase is meant to edit commits being rebased, but it can be used simply to edit commits. It includes 'squash' command that can be used to concatenate (squash) commits.

So to squash last say 5 commits into one, you would use
  $ git rebase --interactive HEAD~5

then edit provided list of commands and commits to read something like this:

   pick deadbee The oneline of this commit
   squash fa1afe1 The oneline of the next commit
   ...
   squash beedead The oneline of the that commit
i.e. replace 'pick' command by 'squash' command.

This is a very powerfull command, and can be used for example to turn series of say 5 commits into series of say 2 commits; not simply squashing to a single commit, but reducing number of commits (and presumably cleaning up those commits).

HTH (hope that helps)
-- 
Jakub Narebski
Poland
Previous: Howard MillerNext: Howard Miller
Message 10 of 14 in “Any way to "flatten" a series of changes in git”
  1. Howard MillerOct 22, 2009
  2. Bill LearOct 22, 2009
  3. Bill LearOct 22, 2009
  4. Jacob HelwigOct 22, 2009
  5. Howard MillerOct 22, 2009
  6. Daniele SegatoOct 26, 2009
  7. Jakub NarebskiOct 22, 2009
  8. Howard MillerOct 22, 2009
  9. Howard MillerOct 22, 2009
  10. Jakub NarebskiOct 22, 2009
  11. Howard MillerOct 23, 2009
  12. Howard MillerOct 23, 2009
  13. Johannes SixtOct 23, 2009
  14. Junio C HamanoOct 23, 2009

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.