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

Re: squash commits deep down in history

From
Jakub Narębski <jnareb@gmail.com>
Date
Oct 24, 2014, 11:43 UTC
Message-ID
<544A3B5A.6030801@gmail.com>
In-Reply-To
<54496036.8080109@kdbg.org>
W dniu 2014-10-23 22:08, Johannes Sixt pisze:
> Am 23.10.2014 um 14:34 schrieb Henning Moll:
Show 15 quoted lines
>> i need to squash several commits into a single one in a automated
>> way. I know that there is interactive rebase, which can also be
>> automated using GIT_SEQUENCE_EDITOR. Unfortunately my history is very
>> large and i need to squash deep down in the history several times. So
>> using interactive rebase seems not to be the right tool.
>>
>> I wonder if i can solve this task using filter-branch? I have a file
>> that list the SHA1s for each squash operation per line. All SHA1s of
>> a line are in chronological order (youngest to oldest), or in other
>> words: the first SHA1 is the child of the second, and so on.
>
> Use git-replace do construct a squashed commit that replaces the last
> child in each run such that its parent points to the parent of the first
> in the run. Then use a git-filter-branch without filters to burn the
> parenthood into the history.

Using grafts (which are only about parentage, and are purely local) with filter-less git-filter-branch might be easier than git-replace.

If you have the following history:
     ... <--- O <--- A <--- B <--- C <--- ...
and you want to squash A, B, C into
     ... <--- O <----------------- C <--- ...
simply add the following to .git/info/grafts
    <sha-1 of C> <sha-1 of O>
for example with
    echo "$(git rev-parse C) $(git rev-parse C^^^)" >>.git/info/grafts

Check if the history is correct e.g. with "git log --graph --oneline", then use filter-branch.

BTW. do you know if it is possible to run filter-branch without replacing replaces?

Just for completeness, yet another way to do scripted history rewriting is to use git-fast-export + some editing script (e.g. reposurgeon[1]) + git-fast-import.

[1]: http://www.catb.org/esr/reposurgeon/
-- 
Jakub Narębski
Previous: Henning MollNext: Phil Hord
Message 4 of 5 in “squash commits deep down in history”
  1. Henning MollOct 23, 2014
  2. Johannes SixtOct 23, 2014
  3. Henning MollOct 23, 2014
  4. Jakub NarębskiOct 24, 2014
  5. Phil HordNov 4, 2014

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.