a06e
a06e

Reputation: 20774

git squash older commits (not last one)

I have a very messy git history. I want to squash a bunch of older commits (not including the last one).

I know how to squash my last n commits. But this is different. Here I have commits consecutively n1 to n2 which I want to squash into one, while after n2 I have a history of commits that I want to preserve up to the last one.

So, if my current history looks like this:

---- n1 --- n2 -------- m

I want to squash n1 to n2 so it ends up looking like this:

---- n1n2 -------- m

where n1n2 is a single commit containing the squashed contents from n1 to n2.

How should I do this? What are the consequences on the history from n2 to m?

That is, will the hash of every commit from n2 to m change as a consequence of what I want to do?

Upvotes: 58

Views: 32528

Answers (2)

Frederik Bode
Frederik Bode

Reputation: 2744

I wrote a bash function to automatically mark commit n2 and all commits between n1 and n2 as fixup. This way you don't have to manually mark every commit with fixup. It supports a DRY_RUN argument that prints the contents of the "interactive rebase file", so you can play around with the values of START and END. START and END indicate the index of the commits: e.g.: If this is the file you want

pick a5f4a0d commit-1
pick 19aab46 commit-2
fixup 1733ea4 commit-3
fixup 827a099 commit-4
pick 10c3f38 commit-5
pick d32d526 commit-6

START=3 and END=4.

The code:

function squash() {
# example use:
# DRY_RUN=1 START=2 END=4 squash HEAD~4
# START=2 END=4 squash HEAD~4
  mkdir -p /tmp/squash
  if [ -n "${DRY_RUN}" ] && [ "${DRY_RUN}" -eq 1 ]
  then
    echo "
sed  -i \"\"  "${START},${END}s/^[^[:space:]]*/fixup/" \$1
echo 
echo ***START***
echo \"\"
cat \$1
echo \"\"
echo ***END***
echo The error message \"error: there was a problem with the editor squash\" is normal when running DRY_RUN=1
exit 1 # force exit to not actually rebase
    " > /tmp/squash/squash
  else
    echo "
sed  -i \"\"  "${START},${END}s/^[^[:space:]]*/fixup/" \$1
    " > /tmp/squash/squash
  fi
  chmod +x /tmp/squash/squash
  OLDPATH=${PATH}
  export PATH="/tmp/squash:${PATH}"
  GIT_SEQUENCE_EDITOR=squash git rebase -i "$@"
  export PATH=${OLDPATH}
}

Upvotes: 2

ludovico
ludovico

Reputation: 2406

You can do an interactive rebase, per the docs and this blog post.

  1. Start an interactive rebase:

    git rebase -i HEAD~n
    

    (where n is how far do you want to go back in history)

  2. Your default editor will open. At the top, a list of your latest n commits will be displayed, in reverse order. Eg:

    pick a5f4a0d commit-1
    pick 19aab46 commit-2
    pick 1733ea4 commit-3
    pick 827a099 commit-4
    pick 10c3f38 commit-5
    pick d32d526 commit-6
    
  3. Specify squash (or the shortcut s) for all commits you want to squash. E.g.:

    pick a5f4a0d commit-1
    pick 19aab46 commit-2
    squash 1733ea4 commit-3
    squash 827a099 commit-4
    pick 10c3f38 commit-5
    pick d32d526 commit-6
    

    Git applies both that change and the change directly before it and makes you merge the commit messages together.

  4. Save and exit.

  5. Git will apply all changes and will open again your editor to merge the three commit messages. You can modify your commit messages or leave it as it is (if so, the commit messages of all commits will be concatenated).

  6. You're done! The commits you selected will all be squashed with the previous one.

Upvotes: 64

Related Questions