Subject: Re: Question: is there a short way to merge the last commit to the second last one?

From: "brian m. carlson" <[email-masked]>
Date: Sun, 8 Jun 2025 18:57:44 +0000

On 2025-06-08 at 18:30:30, Aditya Garg wrote:
> Hi all

Hi,

> This is something I usually come across. Sometimes I make a mistake in
> a commit, and then I create a new commit with a correction. After that
> I git rebase -i and use the fixup option to make the fixup commit a
> part of the main commit.
> 
> I was wondering if there is a command, like git fixup or something
> that could make this process easier? I know about git squash but I
> prefer doing fixup.

You are hardly the only person to have this problem.  It happens to me
and lots of other people very frequently and so we do have a couple ways
to fix that.

If the mistake you want to fix is in the most recent commit, instead of
making a new commit, you can do `git commit --amend`.  That updates the
last commit with the changes you've staged via `git add` and `git rm`.
It also allows you to edit the message; if you don't want that, you can
pass the `--no-edit` option.

The other situation is that the mistake is in an older commit.  Here, we
have a special variant of `git commit` that will mark the commit to be
automatically fixed up.  You could say something like `git commit
--fixup HEAD^` (or whatever revision you like instead of `HEAD^`). Then,
when you do `git rebase -i --autosquash`, it will automatically be moved
to the proper location and marked for fixup.

If you want to change the commit message as well, you can replace
`--fixup` with `--squash` and you'll get prompted for something you can
put in the commit message.  When you squash it, then you'll be prompted
to merge the two commit messages (the old and new).

If your goal is to just do the fixups and squash and not anything else
interactive, then you can do this:

    GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash

in a POSIX shell (that is, on macOS or Linux or under Git Bash, but not
under Powershell or CMD).

The `GIT_SEQUENCE_EDITOR=true` tells Git not to edit the sequence list
(also known as the todo list, which contains the pick, fixup, and squash
commands) and just perform the rebase.  You can see an alias for doing
this at [0], which may be helpful as well.

[0] https://github.com/bk2204/dotfiles/blob/5e74a513ae133ec30b20992fb4c2f967fa6f047b/git/gitconfig#L41
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA