Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amending a Git commit to a shared repo

I have a local git repo which I recently made a commit to, then pushed to a shared repo. Only after I pushed it to the shared repo did I realize I made an ugly mistake. I amended it locally no problem after I fixed my source with:

git commit -C HEAD -a --amend

After that, I tried another git push origin and I get the following error:

! [rejected]        mybranch -> mybranch (non-fast forward)

What's the best way to rectify this situation?

like image 307
Coocoo4Cocoa Avatar asked Apr 30 '09 20:04

Coocoo4Cocoa


People also ask

Can you amend an already pushed commit?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.


1 Answers

git doesn't (by default) allow you to push to a branch anything that "rewinds" the branch tip. In other words, if the current branch head is not a direct parent or ancestor of the branch tip then the push will be refused.

You can try to push anyway by using the -f option to git push or by using a refspec with a leading '+', e.g. git push origin +mybranch:mybranch .

Usually remote repositories will still not let this happen because you risk losing commits if different people can indiscriminately push branch tips that don't include commits that they don't have locally.

You can override this behaviour by changing the configuration parameter receive.denyNonFastForwards on the remote repository (assuming that you have the appropraite access to the remote repository).

If you don't have such access you may be able to achieve this by deleting the remote branch and recreating it.

e.g.

git push origin :mybranch
git push origin mybranch

Note that more recent versions of git include a configuration parameter receive.denyDeletes that will, if set, prevent this potentially dangerous workaround from working.

like image 121
CB Bailey Avatar answered Oct 19 '22 07:10

CB Bailey