Git prevents pushing after amending a commit

GitGit PushGit CommitGit Amend

Git Problem Overview


Usually, I just run

git add file
git commit
git push

but if I amend the commit before pushing it (with git commit --amend), the next push fails with

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

How can I let git push the changes without merging branches? I only have one branch (master) and I'm the only person using this repo so why is it saying this?

git branch -a:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

EDIT: Using gitk HEAD @{u}, I see that I have 2 branches, one with the original commit and another with the amended commit.

Git Solutions


Solution 1 - Git

This should only be the case if you're amending an already-pushed commit. Generally you should never do that as you're then modifying published history. In your case however, you should be able to get away with push -f, which will overwrite the remote commit with your amended revision.

Solution 2 - Git

Yup, you should not do that (pushing a commit, then changing it and trying to push it again).

Instead, you can roll back Git to your previous commit without changing the files, then creating a new commit:

git reset --mixed origin/master
git add .
git commit -m "This is a new commit for what I originally planned to be an amendmend"
git push origin master

this will create a new commit with the changes you were about to amend.

Solution 3 - Git

you amended the pulled commit as in

git pull origin master
git commit -a --amend -m "..."
git push

you can solve the issue by reverting the amended commit:

git reset --mixed origin/master

and then making it again as a full fledged commit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkiriView Question on Stackoverflow
Solution 1 - GitJoeyView Answer on Stackoverflow
Solution 2 - GitNils WernerView Answer on Stackoverflow
Solution 3 - GitStefano FalascaView Answer on Stackoverflow