49

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.

kiri
  • 2,142
  • 3
  • 18
  • 42
  • 1
    possible duplicate of [How do I push amended commit to the remote git repo?](http://stackoverflow.com/questions/253055/how-do-i-push-amended-commit-to-the-remote-git-repo) – Deebster Sep 03 '13 at 09:44
  • @Deebster The question you linked was about amending a commit already pushed to the remote, I'm amending a commit which hasn't been pushed yet. – kiri Sep 03 '13 at 09:52
  • `I only have one branch (master) and I'm the only person using this repo` and `I see that I have 2 branches, one with the original commit and another with the amended commit.` Its conflicting, can you please explain what you exactly did – Sagar Sakre Sep 03 '13 at 10:03
  • `git add file` `git commit -m "something"` `git commit -m "something else"` would work fine – Sagar Sakre Sep 03 '13 at 10:04
  • @Sagar It looks like the amendment has been created as a new branch and the original commit is in `master`. I haven't made a new branch myself – kiri Sep 03 '13 at 10:12
  • No amend wont create any branch. Whats the output of `git branch -a` ? – Sagar Sakre Sep 03 '13 at 10:20
  • @Sagar Added output to my question – kiri Sep 03 '13 at 10:24
  • The problem appears to have fixed itself. I thank everyone who contributed for their time and effort. It was likely fixed by [this answer](http://stackoverflow.com/a/927386/2734389) – kiri Sep 03 '13 at 10:31

3 Answers3

73

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.

Joey
  • 316,376
  • 76
  • 642
  • 652
  • 1
    Can't thank @joey enough, that was the exact reason why I wasted the last few minutes, and yes it did resolve the issue. – Robins Tharakan Aug 09 '17 at 10:46
  • 3
    BEWARE - if not used with caution `git push -f ` can overwrite your commit history. Leaving no way for you to revert commit if something goes wrong. – markroxor Mar 30 '18 at 18:36
  • @markroxor yes. don't must use -f command. Because overwrite all previous commits. This is destroyer. – doğukan Jan 11 '19 at 20:10
  • I also mistakenly ammend'ed after a push. @markroxor can you provide a workaround then, if -f is a bad solution? This would be extra helpful. – foob.ar Feb 15 '19 at 18:47
  • 1
    "Leaving no way for you to revert commit if something goes wrong." Find someone who has not pulled from remote. :/ – markroxor Feb 16 '19 at 05:07
  • @markroxor Or pull yourself again in some other location. – Meet Sinojia Aug 03 '20 at 18:53
27

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.

Nils Werner
  • 28,291
  • 6
  • 62
  • 82
8

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

Stefano Falasca
  • 7,642
  • 1
  • 13
  • 22