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, 21:43 UTC
- Message-ID
- <aEYD5X940Li_H7KU@tapette.crustytoothpaste.net>
- In-Reply-To
- <PN3PR01MB9597D380178A875C6FFA90E0B868A@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM>
On 2025-06-08 at 19:35:24, Aditya Garg wrote:
Show 16 quoted lines
> > > On 9 June 2025 1:01:19 am IST, Phillip Wood <phillip.wood123@gmail.com> wrote: > >On 08/06/2025 19:57, brian m. carlson wrote: > >> > >> 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 > > > >Just a quick note to say that since git v2.44.0 you can just run > > > > git rebase --autosquash > > > > Silly question but how does it get to know what is the fixup commit?
That's actually a great question. When you do `git commit --squash REVISION`, it takes the summary of the commit specified by `REVISION` and precedes it with `squash! `, and for `--fixup`, it does `fixup! `.
Then, it's just a matter of re-ordering the squash or fixup commits in order after the commit with the corresponding summary.
So with this shell script:
---- #!/bin/sh
git init --object-format=sha256 test-repo cd test-repo
echo abc >file.txt git add file.txt git commit -m 'Initial commit'
echo def >file.txt git add file.txt git commit --fixup HEAD git show ----
You get something like this:
---- Initialized empty Git repository in /tmp/user/1000/test-repo/.git/ [dev (root-commit) 7327102] Initial commit 1 file changed, 1 insertion(+) create mode 100644 file.txt [dev 8bdd271] fixup! Initial commit 1 file changed, 1 insertion(+), 1 deletion(-) commit 8bdd271b6d4e22b7ca697c2d4499fd3e0825977d7d2c917b92e1f1f12383f52c Author: brian m. carlson <sandals@crustytoothpaste.net> Date: Sun Jun 8 21:41:43 2025 +0000
fixup! Initial commit
diff --git a/file.txt b/file.txt index e0ef420..559afde 100644 --- a/file.txt +++ b/file.txt @@ -1 +1 @@ -abc +def ---- Of course, `--autosquash` does require using `--fixup` and `--squash` or otherwise naming the commits that way.
-- brian m. carlson (they/them) Toronto, Ontario, CA