Black Friday sale: 30% off Pro and Unlimited, until 22 December

How to tidy unpushed commits in Git before you push

Fold a wip commit into the one it belongs to, fix a message, add a forgotten file, all on local commits only, so the history you push reads clean.

Four commits are waiting on main, and two of them embarrass you: a wip, and a fix that belongs inside an earlier commit. Nobody has seen them yet, so you can still rewrite them. The rule is simple: tidy only commits that are not pushed.

Find the safe range

Refresh your copy of the remote, with pruning off:

git fetch --no-prune --no-prune-tags

Then list what your branch has that its upstream doesn’t.

git log --oneline @{u}..HEAD
6c0d427 Fix typo in README
6d48725 wip
479b129 Fix cart rounding
b89d50b Add coupon field

Everything on this list is yours alone. Everything below it is shared history, and the rest of this page leaves it untouched. If the list is empty, there is nothing to tidy.

Fix the last commit: amend

You forgot a line in the newest commit. Stage it and fold it in:

git add -u
git commit --amend --no-edit

The commit keeps its message and gets a new ID, 6c0d427 becoming b1203dc. Leave --no-edit off to rewrite the message instead.

Fix an older commit: fixup

The rounding fix needs one more line, but that commit is two steps back. Stage the change and record it as a fixup of that commit, found by its message:

git add -u
git commit --fixup "$(git log --format=%h --grep='^Fix cart rounding$' @{u}..HEAD)"
9f37657 fixup! Fix cart rounding
b1203dc Fix typo in README
6d48725 wip
479b129 Fix cart rounding
b89d50b Add coupon field

The new commit sits at the top for now. Autosquash moves it into place.

Fold it in: autosquash

Rebase onto the upstream. Only the commits above it are replayed.

git rebase -i --autosquash @{u}

Git opens your editor with the plan already arranged. The fixup moved up under its target, marked to be folded in:

pick b89d50b Add coupon field
pick 479b129 Fix cart rounding
fixup 9f37657 fixup! Fix cart rounding
pick 6d48725 wip
pick b1203dc Fix typo in README

Save and close. The fixup disappears into Fix cart rounding, and your range is one commit shorter:

62e7b98 Fix typo in README
841170b wip
0376555 Fix cart rounding
b89d50b Add coupon field

To skip the editor next time, run with GIT_SEQUENCE_EDITOR=true, or turn on git config rebase.autoSquash true so -i always arranges fixups.

Fold a wip commit away

Change pick to fixup on the wip line, in the editor of a new git rebase -i @{u}, and it melts into the commit above it, dropping its message. Use squash instead to keep both messages and edit them together. Use reword to change one message only.

pick b89d50b Add coupon field
pick 0376555 Fix cart rounding
fixup 841170b wip
pick 62e7b98 Fix typo in README

Afterwards:

2058441 Fix typo in README
1ad374f Fix cart rounding
b89d50b Add coupon field

A wip that changed a different file than the commit above it still folds in. Make sure the result is the commit you want to read in a year.

If it goes wrong

A conflict stops the rebase halfway. Fix the files, git add them and run git rebase --continue, or put everything back as it was:

git rebase --abort

If the rebase already finished and you dislike the result, Git remembers where the branch stood before:

git reset --hard ORIG_HEAD

This also throws away uncommitted edits. Commit or stash them first.

ORIG_HEAD is overwritten by the next rebase or merge, so use it right away. After that, git reflog lists earlier positions of the branch.

Never on pushed history

Rewriting a commit gives it a new ID. If the old one is already on the remote, the next push is rejected, and forcing it past the rejection rewrites what your teammates already pulled. That is why every command above starts from @{u}: the range stops where the shared history begins.

Before you rebase, check what a push would send, as in how to see what you are about to push. To check every repository at once, see how to find unpushed commits.

Or see it before you click

push& (Pushand) is a Mac menu bar app that counts the unpushed commits on every branch and lists them, so you know which ones are still yours to tidy. Click a branch to send it. Free for 3 repositories, and every install starts with a 14-day trial, no card and no account.

See how it works

More Git how-tos