42

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?

a06e
  • 18,594
  • 33
  • 93
  • 169
  • 1
    1. Interactive rebase, see https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History. 2. Every commit from n1, which is also rewritten, yes. – jonrsharpe May 22 '19 at 18:12
  • @jonrsharpe Just to confirm, ALL commits from`n2+1` to `m` will also get new hashes? – a06e May 22 '19 at 23:04
  • 1
    Yes, because they all have new parent commits now. – jonrsharpe May 23 '19 at 06:26
  • @jonrsharpe So commits `1,...,n1` will preserve their hash, even if some of them are included in the interactive rebase but with `pick`. – a06e May 23 '19 at 12:14
  • Not necessarily, a few things go into the commit including a timestamp: https://stackoverflow.com/a/28917694/3001761. – jonrsharpe May 23 '19 at 12:17

1 Answers1

49

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ludovico
  • 2,103
  • 1
  • 13
  • 32
  • 9
    Just to confirm. In this case example you put, commits 2, 3, 4 will be squashed into a single commit, let's call it commit `2*`. My new history will look contain the commits `1, 2*, 5, 6`. In addition, `1` will preserve its old SHA-1, but `2*, 5, 6` will all get new SHA-1 hashes. Am I correct? – a06e May 22 '19 at 23:08
  • 6
    @becko correct -- commit 2* gets a new hash because the contents of the commit are now different; commits 5 and 6 get a new hash because their parent commit IDs have changed. For more details: https://stackoverflow.com/a/32854964/11298742 – ludovico May 23 '19 at 12:22
  • And then how do I push this, if the commits I have just squashed were already pushed to remote? After editing the commit message, I see "Your branch and 'origin/main' have diverged, and have 4 and 10 different commits each, respectively. (use "git pull" to merge the remote branch into yours) – pretzlstyle Jul 03 '22 at 18:42
  • @pretzlstyle an interactive rebase changes git commit history. If you have already pushed and you're working with other developers, you should avoid modifying git commit history as it can have important implications. If you understand those implications, --force would be an option (Warning: "Do not use the --force flag unless you’re absolutely sure you know what you’re doing") – ludovico Jul 20 '22 at 13:57