Search code examples
gitgit-commitgit-push

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


Let's say this is my class that was in my staged area while committing and pushing to the remote repository:

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

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

After that, I realized I didn't add getters and setters that should be included in this last commit.

Is there a way to change the last commit (add getters & setters) that has already been pushed to the remote repository ?

Or should I just add a new commit with some message indicating that this is a refinement of a previous commit ?


Solution

  • Best way is to make new commit with fix. It`s very important thing to keep history clear.

    But only for education purposes: You can make commit with --amend flag and then make push with --force flag. You got an error because git tried to save you remote repository clean. And you should told that you understand what you want to do, by adding --force git push --force