我可以更改推送到远程存储库的最后一次提交吗?

Can I change the last commit pushed to the remote repository?

假设这是我的 class 在提交和推送到远程存储库时在我的临时区域中:

public class Money {
    private int amount;
    private String currencyCode;

    public Money(int amount, String currencyCode) {
        this.amount = amount;
        this.currencyCode = currencyCode;
    }
}

在那之后,我意识到我没有添加应该包含在最后一次提交中的 getter 和 setter。

有没有办法更改已推送到远程存储库的最后一次提交(添加 getter 和 setter)?

或者我应该只添加一个新的提交,其中包含一些消息,表明这是对先前提交的改进?

最好的方法是通过修复进行新的提交。保持历史清晰是非常重要的事情。

但仅用于教育目的: 您可以使用 --amend 标志进行提交,然后使用 --force 标志进行推送。您收到错误消息是因为 git 试图将您的远程存储库保存为干净的。你应该通过添加 --force git push --force

来告诉你你明白你想做什么