我可以从上次推送的提交更新文件吗?

问题描述

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

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

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

在那之后,我意识到我没有添加应该包含在最后一次提交中的 getter 和 setter。当我尝试使用 git commit --amend 执行此操作时,我收到此错误

 error: Failed to push some refs to 'https://github.com/KarolXX/Learn-Java.git'
hint: Updates were rejected because the tip of your current branch is behind
To https://github.com/KarolXX/Learn-Java.git
!   refs/heads/master:refs/heads/master [rejected] (non-fast-forward)
hint: its remote counterpart. Integrate the remote changes (e.g.
Done
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我怎样才能实现我的目标?

解决方法

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

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

告诉您您了解自己想要做什么