Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git prevents pushing after amending a commit

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.

like image 213
kiri Avatar asked Sep 03 '13 09:09

kiri


People also ask

Can you push an amended commit?

Amended force push A commit is often amended to update the commit message or add new changes. Once a commit is amended a git push will fail because Git will see the amended commit and the remote commit as diverged content. The --force option must be used to push an amended commit.

What happens when you amend a commit?

Amended commits are actually entirely new commits and the previous commit will no longer be on your current branch. This has the same consequences as resetting a public snapshot. Avoid amending a commit that other developers have based their work on.

How do you push new changes to existing commit?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.

How can we rectify once the commit has been done?

If you only want to undo some of the changes from an earlier commit, you can use a combination of commands we've seen before: $ git revert -n commit $ git reset $ git add -p $ git commit $ git checkout .


2 Answers

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.

like image 122
Joey Avatar answered Sep 18 '22 15:09

Joey


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.

like image 38
Nils Werner Avatar answered Sep 18 '22 15:09

Nils Werner