git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jun 8, 2025, 18:57 UTC
Message-ID
<aEXdKMjAbry1aTXe@tapette.crustytoothpaste.net>
In-Reply-To
<PN3PR01MB959708ED22FE7FE70C17C852B868A@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM>
On 2025-06-08 at 18:30:30, Aditya Garg wrote:
> Hi all
Hi,
Show 8 quoted lines
> 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
Previous: Aditya GargNext: Aditya Garg
Message 2 of 10 in “Question: is there a short way to merge the last commit to the second last one?”
  1. Aditya GargJun 8, 2025
  2. brian m. carlsonJun 8, 2025
  3. Aditya GargJun 8, 2025
  4. Phillip WoodJun 8, 2025
  5. Aditya GargJun 8, 2025
  6. brian m. carlsonJun 8, 2025
  7. Aditya GargJun 9, 2025
  8. Phillip WoodJun 9, 2025
  9. Aditya GargJun 9, 2025
  10. Junio C HamanoJun 8, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.