Git Squash Commits - Interactive Rebase, merge --squash and reset --soft Compared, with Real Output, the Force-Push Rejection, and the Undo with ORIG_HEAD

TL;DR

  • Squashing means replacing N commits with one commit that has the same final file contents. Git has no git squash command; you get it from git rebase -i, git merge --squash or git reset --soft.
  • Interactive rebase (git rebase -i main): keep pick on the first line, change the lines below it to squash (keep every message) or fixup (throw the messages away). The first line can never be squash: Git stops with error: cannot 'squash' without a previous commit.
  • git merge --squash feature stages the combined change on the branch you are on and does not commit, move HEAD, or record a merge. The source branch is left untouched, so git branch -d later refuses it as "not fully merged".
  • git reset --soft <base> plus git commit is the shortest route for the last N commits. The Git reset reference documents git reset --soft HEAD~5; git commit as the way to combine the last 5 commits into 1.
  • After squashing a branch you already pushed, a plain git push is rejected (non-fast-forward). Use git push --force-with-lease, which only overwrites the remote if it is still where you last saw it.
  • To undo: git reset --hard ORIG_HEAD right after the rebase, or find the old tip in git reflog.
  • Not your exact case? If you want to run tests on every commit instead of merging them, read git rebase --exec; if you are not sure what reset --soft does to your index, read git reset soft, mixed and hard.

Table of Content

  1. What squash actually does
  2. The lab branch
  3. Route 1: interactive rebase with squash
  4. Route 2: git merge --squash
  5. Route 3: git reset --soft
  6. fixup and autosquash
  7. Pushing a squashed branch and undoing a squash
  8. Which route to choose
  9. What broke when I ran this
  10. Conclusion

1. What squash actually does

A squash does not delete any file change. It takes a run of commits, keeps their combined effect on the files, and records that effect as a single commit with a single message. The four commits "add login form", "fix typo", "add validation" and "wip" become one commit called "add login form" (or whatever message you write) whose tree is byte-for-byte the tree of the old tip. I checked that after my run with git diff --stat main HEAD, which reported one file changed, 4 insertions, exactly what the four old commits added together.

Why do it? A pull request with four commits where two of them are "fix typo" and "wip" makes the reviewer read your thinking instead of your result, and it makes git bisect stop on a commit that was never meant to stand alone. Squashing before the merge keeps the history on main one commit per change. The trade-off is real: you lose the step-by-step history, and if the branch was already pushed and someone else pulled it, you rewrite history under them (section 7).

Three ways to squash four commits into one commit: interactive rebase, merge --squash and reset --soft
The three squash routes side by side: what you type, what changes, and what is left behind

RouteCommandRewrites the feature branch?Touches main?Message you get
Interactive rebasegit rebase -i mainyes, in placenoall squashed messages concatenated, then you edit
merge --squashgit merge --squash featurenoyes, one new commityou write it
reset --softgit reset --soft main then git commityes, in placenoyou write it
fixup + autosquashgit commit --fixup <sha> then git rebase -i --autosquash mainyes, in placenothe target commit's message is kept

The wording of each option comes from the official pages: git rebase, git merge, git reset and git commit.

2. The lab branch

Every run below used git 2.43.0 on 2026-12-02. I built one repository with an initial commit on main and a feature/login branch with four commits, then copied the directory three times so each route started from the same state. I also set core.editor to true (the shell command that does nothing and succeeds), so every commit-message editor accepted the proposed text without opening. That matters for the output you see: the messages are the defaults Git proposed, not messages I edited.

1$ git --version
2git version 2.43.0
3$ git log --oneline main..HEAD
4b00a5cb wip
5b4eb9ce add validation
63c6463c fix typo
75591300 add login form

Four commits ahead of main, newest first. If you want to follow along, the commands to build it are four times echo ... >> login.txt; git commit -am "..." on a branch created with git switch -c feature/login. If branches are new to you, my Git branching explained post covers switch, merge and the -d versus -D error.

3. Route 1: interactive rebase with squash

This is the route in the video, and the one you will see in most team guides. git rebase -i main opens an instruction list with one pick line per commit, oldest first. You change pick to squash (or s) on every commit you want to melt into the one above it. I scripted that edit so the output is reproducible: GIT_SEQUENCE_EDITOR replaces the editor that opens the list, and my sed expression rewrites pick to squash on every line from line 2 down. By hand you would open the same list in your editor and change the words yourself.

1$ GIT_SEQUENCE_EDITOR="sed -i -e '2,\$s/^pick/squash/'" git rebase -i main
2 1 file changed, 4 insertions(+)
3 create mode 100644 login.txt
4Successfully rebased and updated refs/heads/feature/login.
5$ git log --oneline main..HEAD
64458916 add login form

Four commits became one, and the hash 4458916 is new: even the first commit was recreated, because its content and its parent-plus-message identity changed. Because the editor was set to true, Git accepted its proposed message, which is all four messages stacked:

1$ git log -1 --format=%B
2add login form
3
4fix typo
5
6add validation
7
8wip

That is the first thing to fix in real use: do not accept this message. Open the editor (leave core.editor alone), delete the lines for "fix typo" and "wip", and write one sentence about what the change does. The git rebase reference describes the same interactive list and its commands.

Two details I confirmed in the run. The author of the squashed commit is the author of the first commit in the list (git log --format='%h %an %s' printed 4458916 Rahul Wagh add login form). And the squash only melts a commit into the one above it, so you can squash in groups: pick, squash, pick, squash, squash gives two commits.

4. Route 2: git merge --squash

The second route does not rewrite the feature branch at all. You stay on main, ask Git to merge the branch as if it were one change, and commit it yourself. The git merge reference says --squash produces the working tree and index state as if a real merge happened, "but do not actually make a commit, move the HEAD, or record $GIT_DIR/MERGE_HEAD".

 1$ git switch main
 2$ git merge --squash feature/login
 3Updating 25b17a6..b00a5cb
 4Fast-forward
 5Squash commit -- not updating HEAD
 6 login.txt | 4 ++++
 7 1 file changed, 4 insertions(+)
 8$ git status -s
 9A  login.txt
10$ git commit -m "Add login form with validation"
11$ git log --oneline --graph --all
12* 37472ab Add login form with validation
13| * b00a5cb wip
14| * b4eb9ce add validation
15| * 3c6463c fix typo
16| * 5591300 add login form
17|/  
18* 25b17a6 initial commit

Read the graph: main now has one new commit (37472ab), and the four original commits still exist on feature/login, branching off the same parent. Git printed the word Fast-forward in the middle of the output even though it did not fast-forward anything; the line Squash commit -- not updating HEAD right below it is the truth. This is how the GitHub "Squash and merge" button behaves: one commit on the target branch, and the source branch left alone.

The consequence shows up when you clean up. The branch is not an ancestor of main, so the safe delete refuses it:

1$ git branch -d feature/login
2error: the branch 'feature/login' is not fully merged.
3If you are sure you want to delete it, run 'git branch -D feature/login'
4$ git branch -D feature/login
5Deleted branch feature/login (was b00a5cb).

That is correct behaviour, not a bug, but it means you must verify by eye that the squash commit is on main before you use -D. The tip hash in the last line, b00a5cb, is also your way back: it stays in git reflog for a while.

5. Route 3: git reset --soft

When the commits you want to squash are the last N on your current branch and nobody else has pulled them, git reset --soft is the shortest route. The git reset reference explains that --soft leaves your working tree files and the index unchanged, and gives git reset --soft HEAD~5; git commit as the example for combining the last 5 commits into 1. My four commits sit on top of main, so I reset to main instead of counting:

 1$ git reset --soft main
 2$ git status -s
 3A  login.txt
 4$ git commit -m "Add login form with validation"
 5$ git log --oneline main..HEAD
 637472ab Add login form with validation
 7$ git reflog | head -4
 837472ab HEAD@{0}: commit: Add login form with validation
 925b17a6 HEAD@{1}: reset: moving to main
10b00a5cb HEAD@{2}: commit: wip
11b4eb9ce HEAD@{3}: commit: add validation

After the reset, git status -s shows A login.txt: the change is already staged, so nothing is lost and there is nothing to add again. The reflog at the bottom is the receipt: the old tip b00a5cb is two entries back. The hash 37472ab equals the one from route 2, because the tree, parent, author, message and timestamp second all matched in my run; do not expect that in real life, since the timestamps will differ.

The catch with this route is the base. git reset --soft main only works as a squash if main has not moved since you branched. If it has, you are also resetting away the commits that landed on main in the meantime, and your single commit will silently undo them. Use git merge-base main HEAD as the reset target when you are not sure, or rebase on main first. My post on git reset soft, mixed and hard shows what each mode does to the index and the working tree.

6. fixup and autosquash

If you already know a commit is a correction of an earlier one, you do not have to edit the instruction list by hand. git commit --fixup <sha> creates a commit titled fixup! <original title>, and git rebase -i --autosquash moves it directly under its target and marks it fixup. The git rebase reference says the recommended way to create these markers is the --squash, --fixup and related options of git commit, and that setting rebase.autoSquash to true makes auto-squashing the default for interactive rebase.

 1$ git commit -qa --fixup HEAD~1
 2$ git log --oneline main..
 3added53 fixup! add a
 4cb8ecdb add b
 5436f671 add a
 6$ GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash main
 7Successfully rebased and updated refs/heads/f2.
 8$ git log --oneline main..
 9f2011d0 add b
1023b5bc9 add a

The fixup commit was created last, but --autosquash reordered it next to add a and folded it in. I set GIT_SEQUENCE_EDITOR=true so the list was accepted unchanged: with --autosquash there was nothing left for me to edit. This is the workflow I recommend for review feedback on a pull request: one --fixup commit per comment, then one autosquash before the merge. Note that fixup drops the fixup commit's message, while squash keeps it and opens the editor so you can combine them.

7. Pushing a squashed branch and undoing a squash

All three in-place routes (rebase, reset and autosquash) create new commits with new hashes. If the old ones were already pushed, the remote branch is now not an ancestor of your local one, and a normal push is refused. I reproduced it with a bare repository as origin: three commits c1, c2, c3 pushed, then squashed locally.

 1$ GIT_SEQUENCE_EDITOR="sed -i -e '2,\$s/^pick/squash/'" git rebase -i main
 2Successfully rebased and updated refs/heads/feat.
 3$ git push origin feat
 4 ! [rejected]        feat -> feat (non-fast-forward)
 5error: failed to push some refs to '/tmp/lab/remote.git'
 6hint: Updates were rejected because the tip of your current branch is behind
 7hint: its remote counterpart. If you want to integrate the remote changes,
 8hint: use 'git pull' before pushing again.
 9$ git push --force-with-lease origin feat
10 + 5819e14...3e505f9 feat -> feat (forced update)

The hint tells you to git pull. Do not follow it here: pulling would merge your old three commits back in and undo the squash. What you want is to overwrite the remote branch on purpose. The git push reference describes --force-with-lease as overriding the non-ancestor restriction only if the current value of the remote ref is the expected value; otherwise the push fails. That is the difference from --force: if a teammate pushed to the branch after your last fetch, the lease check fails instead of destroying their commit. The + in the last line, with the old and new tips, is Git telling you a forced update happened.

To undo a squash, the old tip is still reachable. Right after a rebase, ORIG_HEAD points at it. The git rebase page says that ORIG_HEAD is set to the tip of the to-be-rebased branch when the rebase starts, but is not guaranteed to still point there if other commands that change ORIG_HEAD (like git reset) are used afterwards. I used it immediately:

1$ git reset --hard ORIG_HEAD
2$ git log --oneline main..
35819e14 c3
45de337b c2
544f5d07 c1
6$ git reflog | head -3
75819e14 HEAD@{0}: reset: moving to ORIG_HEAD
83e505f9 HEAD@{1}: rebase (finish): returning to refs/heads/feat
93e505f9 HEAD@{2}: rebase (squash): c1

The three original commits are back with their original hashes, because they never stopped existing. If you have done other resets since, find the old tip in git reflog and run git reset --hard <sha> on it.

SituationCommandWhat it protects against
Branch rewritten, push rejectedgit push --force-with-lease origin <branch>overwriting commits a teammate pushed after your last fetch
Squash went wrong, just did itgit reset --hard ORIG_HEADlosing the original commits
Squash went wrong, some time agogit reflog, then git reset --hard <sha>the same, as long as the reflog entry still exists
Branch shared with othersdo not squash; use route 2 at merge timerewriting history under people's clones

8. Which route to choose

On a four-commit branch like the lab one, the interactive rebase took 26 ms, which is why I never think about speed for ordinary pull requests. To see how it scales I measured the three routes on a branch of 200 one-line commits (git 2.43.0, 2026-12-02, a small local repository, so absolute numbers on your project will be larger). Interactive rebase with every line after the first set to squash took 340 ms (about 1.7 ms per commit), git reset --soft main plus git commit took 9 ms, and git merge --squash plus git commit took 11 ms. All three produced a tree identical to the old tip (git diff --quiet <old-tip> HEAD printed nothing). Speed is not the reason to choose one: the rebase replays every commit, so it grows with the number of commits and with conflicts, while the other two touch only the final state.

I use route 2 (or the "Squash and merge" button, which does the same) when the branch is a pull request and I want main to get one commit per change without touching the branch my reviewer is looking at. I use autosquash when I am answering review comments. I use route 3 for my own unpushed work where I know the base has not moved. I use plain interactive rebase when I want to squash some commits and keep others, because it is the only route that can leave two or three commits.

If you are choosing a team policy, the question is not "which command" but "who is allowed to rewrite which branch". Squash on the feature branch before merge, never on shared long-lived branches such as main. The rest of the course, from branches to pull requests, is in the free Git and GitHub blueprint course.

9. What broke when I ran this

These are the things that actually went wrong or surprised me in the runs above.

1. A squash on the first line. I changed every line to squash, including the first, to see what Git says. It stops before changing anything:

1$ GIT_SEQUENCE_EDITOR="sed -i -e '1s/^pick/squash/'" git rebase -i main
2error: cannot 'squash' without a previous commit
3You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
4Or you can abort the rebase with 'git rebase --abort'.

The fix is what the message says: keep pick on the oldest commit, because squash needs a commit above it to melt into. In my run the branch was unchanged after git rebase --abort.

2. The pull hint after the rejected push. Git suggested git pull for a push that was rejected only because I had rewritten history. Following that hint would have merged my three old commits back next to the squashed one. Use --force-with-lease instead, and only on a branch you own.

3. git branch -d refusing a branch I had just merged. After git merge --squash, Git does not know the branch was merged, because no merge commit ties the two histories together. The error was the branch 'feature/login' is not fully merged. Check that the squash commit is on main, then delete with -D. My post on deleting merged branches lists commands that assume real merges and therefore miss squash-merged branches for the same reason.

4. A misleading Fast-forward line. git merge --squash printed Fast-forward and then Squash commit -- not updating HEAD. Nothing moved. Trust the second line and git status, not the first.

5. A squash message that is four messages. With the editor suppressed, my commit message was all four messages stacked, including "wip". A squash that ships with "wip" in its message is the most common way a clean history gets dirty again. Always read the editor.

10. Conclusion

Squashing is three different tools wearing one name. Interactive rebase edits the branch in place and is the most flexible, git merge --squash leaves the branch alone and is what the merge button does, and git reset --soft is the shortest way to collapse unpushed commits. After any in-place squash of pushed commits, push with --force-with-lease, and keep ORIG_HEAD and the reflog in mind as your undo.

Things to check before you squash:

  1. Is the branch pushed and used by anyone else? If yes, do not rewrite it.
  2. Has main moved since you branched? If yes, rebase first, or do not use reset --soft main.
  3. Did you delete the "wip" and "fix typo" lines from the proposed message?
  4. After the squash, does git diff <old-tip> HEAD print nothing? That proves the content is identical.

This is lesson Day 9 of the DevOps Master Course on YouTube, and the written foundation for rebasing, branches and pull requests is in the free Git and GitHub blueprint course.

Did this save you a debugging session? Stay in the loop -