A command-line tool that takes the current staged or unstaged changes in the branch and ammends a previously added commit with the changes. It preserves commit naming and order.
With Go installed and $(go env GOPATH)/bin on your PATH:
go install github.com/ramzis/amend-commit@latest
This drops the binary as amend-commit into $(go env GOPATH)/bin.
Alternatively, clone the repo and use the included Taskfile:
git clone https://github.com/ramzis/amend-commit.git
cd amend-commit
task install
amend-commit -[number]
where number is an optional flag with a non-negative integer representing the Nth from last commit to amend.
amend-commit auto
amends each file in its respective previous commit or logs warning for the files that haven't been commited yet.
-
amend-commit Amends the last commit.
-
amend-commit -1 Amends the commit before the last one.
-
amend-commit -N Amends the Nth commit and so on.
-
amend-commit auto Amends each file into a previous commit where it was edited.
- If using
autoand a previous commit for that file does not exist, it logs a warning and does not amend it. - If a commit is from the base branch, it logs a warning and does not amend it.
- If a commit amends files that were edited it other commits on this branch, it logs a warning and does not amend it. This enforces the convention that each commit in a feature branch modifies a file once.
The tool includes a Go test suite that runs the command line tool with various branch states and checks the correct behaviour.
Example test case:
Commit history (latest is first): COMMIT A (changes to files: A) COMMIT B (changes to files: B)
- Changes: Add line to file A.
- Run
amend-commit. Expected: changes applied to commit A since it previously edited file A. - Run
amend-commit -1. Expected: no changes, warning since another commit on the branch (commit A) already touched the same file. - Changes: Add line to file B.
- Run
amend-commit. Expected: no changes, warning since another commit on the branch (commit B) already touched the same file. - Run
amend-commit -1. Expected: changes applied to commit B since it previously edited file B.
Example test case using auto:
Commit history (latest is first): COMMIT A (changes to files: A) COMMIT B (changes to files: B)
- Changes: Add line to file A.
- Changes: Add line to file B.
- Changes: Add line to file C.
- Changes: Add line to file D.
- Run
amend-commit auto. Expected: commit A gets changes for file A. commit B gets changes for file B. a warning is logged for file C, D.