-2

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 ?

Monoxyd
  • 406
  • 1
  • 7
  • 18
  • What is your goal? You have pushed commits to github, but you amended the last commit, now you want to push again with amended commit? – Amith Jul 08 '21 at 11:59
  • 1
    This is already answered here I believe: https://stackoverflow.com/questions/253055/how-do-i-push-amended-commit-to-the-remote-git-repository – Amith Jul 08 '21 at 12:01
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd Jul 08 '21 at 12:49

1 Answers1

2

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

  • 1
    "It`s very important thing to keep history clear." - I know that's why I didn't want to create a new commit with such a small change (it's just a small tweak). In my opinion the history would be clearer without such small commits, am I wrong? – Monoxyd Jul 09 '21 at 08:29
  • Don't be afraid to fix mistakes with new commits. It is more respectful. Just be sure that pull requests to your master branch have all changes you want to apply – Gilevich Petr Jul 09 '21 at 09:21