0

In this question Git prevents pushing after amending a commit it is been mentioned that:

This should only be the case if you're amending an already-pushed commit

But what I did was the steps below:

  1. Pushed a bunch of code
  2. commit --amend
  3. commit --amend
  4. commit --amend
  5. Pushed
  6. Pull (CONFLICT (content))

This conflict can happend in the future too, as I don't understand why did conflict happen! Could someone shed some light on the process?


In step 4:

$ git commit --amend
[dev cf0f21d] blahhh(blah) is added
 Date: Wed Sep 30 08:39:28 2015 +0330
 5 files changed, 168 insertions(+), 1 deletion(-)

In step 5:

$ git push origin dev
To repo:~/something
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'repo:~/something'
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.
Community
  • 1
  • 1
Alireza
  • 6,497
  • 13
  • 59
  • 132

1 Answers1

1

In steps 2, 3, 4 you are amending a commit which is already pushed in 1

Commit amend amends the last commit in the repo. Hence you are amending the commit you already pushed in 1. You need to create a new commit after (1), and then you can amend that commit however you wish.

Also, in step 5 you should have got a warning or you most probably did a force-push

Harish Ved
  • 560
  • 4
  • 17
  • I've added some files to `staged` in step 2,3 and 4 and then used `commit --amend`. What's wrong with it? – Alireza Sep 30 '15 at 08:36
  • In step 5 I did not force-push. No warning. – Alireza Sep 30 '15 at 08:38
  • Those staged files were 'committed' to the last commit in your working tree. As it 'amend'ed the last commit. And you had already pushed your last commit in (1) – Harish Ved Sep 30 '15 at 08:38
  • When I pushed it is rejected, so why issuing `git pull` gave me a conflict? – Alireza Sep 30 '15 at 09:17
  • Because locally you had that 'amend'ed commit. The remote had the original commit along with any new changes. So, git pull should will fail because both local and remote are at different HEADs. – Harish Ved Sep 30 '15 at 09:27
  • **local** -- commit1 -> commit2 **remote** -- commit1 -> commit2 _after step 2_ **local** -- commit1 -> amendedcommit2 **remote** -- commit1 -> commit2. So when you do a git pull, it will be confused as to what should happen to 'amendedcommit2' and where it should place 'commit2' (which is now new for your local working tree) – Harish Ved Sep 30 '15 at 09:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90970/discussion-between-alireza-hos-and-harish-ved). – Alireza Sep 30 '15 at 09:30