I did some modifications to the AGENTS.md file in my GitHub repo and I want to add this change to the commit that added this file in my feature branch. Normally I would:
- Stash my changes
- Run
git rebase -i mainand mark the target commit withedit - When the rebase stops at the target commit, I would apply my stashed changes and amend the commit with
git commit --amend - Finally, I would continue the rebase with
git rebase --continue
But there is a much easier way to do this! I can use the --fixup option of git commit to create a new commit that will be automatically squashed into the target commit during a rebase.
Here's an example commit history of my feature branch:
a22cedc fix: add default target
ac42e65 fix: refactor service_test.go with test fixture and grouping
d03eec5 fix: use interface for client creator
848a195 fix: refactor diff_generator_test.go
74ae314 feat: add AGENTS.md and rename readme.md to README.md
fbd3df2 fix: refactor diffable_test.go to use fixture
57cf567 feat: add cover target to Makefile
69fdec3 feat(ci): add coverage diff check for PRs
60f6ae1 (tag: v1.3.0, origin/main, origin/HEAD, main) Merge pull request #38 from fd/feat/makefile
I want to commit my changes to the commit with the message feat: add AGENTS.md and rename readme.md to README.md, which has the hash 74ae314. To do this, you can run either of the following commands:
gci --fixup=74ae314
# or
git commit --fixup=':/AGENTS.md'The second form is very convenient because it allows you to specify the commit by searching for a string in the commit message. In this case, :/AGENTS.md will find the commit that contains "AGENTS.md" in its message.
Then, when you are ready to squash the fixup commit into the target commit, you can run:
git rebase -i --autosquash mainAnd you are done!