How do I push amended commit to the remote Git repository?

git push
git commit and push
git push amend
git commit --amend
git overwrite pushed commit
git push -u option
git force push
git push commit to pull request

When I've worked a bit with my source code, I did my usual thing commit and then I pushed to a remote repository. But then I noticed I forgot to organize my imports in the source code. So I do the amend command to replace the previous commit:

> git commit --amend

Unfortunately the commit can't be pushed back to the repository. It is rejected like this:

> git push origin
To //my.remote.repo.com/stuff.git/
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '//my.remote.repo.com/stuff.git/'

What should I do? (I can access the remote repository.)

I actually once pushed with --force and .git repository and got scolded by Linus BIG TIME. In general this will create a lot of problems for other people. A simple answer is "Don't do it".

I see others gave the recipe for doing so anyway, so I won't repeat them here. But here is a tip to recover from the situation after you have pushed out the amended commit with --force (or +master).

  1. Use git reflog to find the old commit that you amended (call it old, and we'll call the new commit you created by amending new).
  2. Create a merge between old and new, recording the tree of new, like git checkout new && git merge -s ours old.
  3. Merge that to your master with git merge master
  4. Update your master with the result with git push . HEAD:master
  5. Push the result out.

Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push will see the resulting merge will see that you favor new over old. Their later merges will not see the conflicts between old and new that resulted from your amending, so they do not have to suffer.

How do I push amended commit to the remote Git repository?, Here don't push --force to .git which will delete your history. But here is a tip to recover from the situation after you have pushed out the� Here don’t push --force to .git which will delete your history. But here is a tip to recover from the situation after you have pushed out the amended commit with --force. Use git reflog to find the old commit that you amended. Create a merge between old and new, using git checkout new && git merge -s ours old.

You are seeing a Git safety feature. Git refuses to update the remote branch with your branch, because your branch's head commit is not a direct descendent of the current head commit of the branch that you are pushing to.

If this were not the case, then two people pushing to the same repository at about the same time would not know that there was a new commit coming in at the same time and whoever pushed last would lose the work of the previous pusher without either of them realising this.

If you know that you are the only person pushing and you want to push an amended commit or push a commit that winds back the branch, you can 'force' Git to update the remote branch by using the -f switch.

git push -f origin master

Even this may not work as Git allows remote repositories to refuse non-fastforward pushes at the far end by using the configuration variable receive.denynonfastforwards. If this is the case the rejection reason will look like this (note the 'remote rejected' part):

 ! [remote rejected] master -> master (non-fast forward)

To get around this, you either need to change the remote repository's configuration or as a dirty hack you can delete and recreate the branch thus:

git push origin :master
git push origin master

In general the last parameter to git push uses the format <local_ref>:<remote_ref>, where local_ref is the name of the branch on the local repository and remote_ref is the name of the branch on the remote repository. This command pair uses two shorthands. :master has a null local_ref which means push a null branch to the remote side master, i.e. delete the remote branch. A branch name with no : means push the local branch with the given name to the remote branch with the same name. master in this situation is short for master:master.

How do I push amended commit to the remote Git repository , Amending a commit is part of a class of git operations that are said to “rewrite history”. In essence, you are modifying some aspect of your commit to contradict� 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.

Quick rant: The fact that no one has posted the simple answer here demonstrates the desperate user-hostility exhibited by the Git CLI.

Anyway, the "obvious" way to do this, assuming you haven't tried to force the push, is to pull first. This pulls the change that you amended (and so no longer have) so that you have it again.

Once you have resolved any conflicts, you can push again.

So:

git pull

If you get errors in pull, maybe something is wrong in your local repository configuration (I had a wrong ref in the .git/config branch section).

And after

git push

Maybe you will get an extra commit with the subject telling about a "Trivial merge".

How to push amended commit to the remote Git repository, Pushing is how you transfer commits from your local repository to a remote repo. Learn how to Remote branches are configured using the git remote command. Pushing has The --force option must be used to push an amended commit. If you look at the Git history, you can the “Create readme.md” commit is on the local branch, but this commit did not make it to the remote yet. To push the new commit onto the remote, you need to click on the push button again. Then, click push once more. That’s it. Once the commit is pushed to the remote branch, you can see that the

Short answer: Don't push amended commits to a public repo.

Long answer: A few Git commands, like git commit --amend and git rebase, actually rewrite the history graph. This is fine as long as you haven't published your changes, but once you do, you really shouldn't be mucking around with the history, because if someone already got your changes, then when they try to pull again, it might fail. Instead of amending a commit, you should just make a new commit with the changes.

However, if you really, really want to push an amended commit, you can do so like this:

$ git push origin +master:master

The leading + sign will force the push to occur, even if it doesn't result in a "fast-forward" commit. (A fast-forward commit occurs when the changes you are pushing are a direct descendant of the changes already in the public repo.)

Git Push, Option 1: Add another commit and push to your PR branch If you're using a different naming scheme for origin and upstream , you will master $ git pull https://github.com/your-username/repo.git master $ echo 'This PR� Your Git commit message should now be amended on the remote repository. Conclusion. In this tutorial, you learnt how you can easily amend a Git commit message whether it has already been pushed or not. You learnt that you can either modify the last Git commit with the “–amend” option, or you can modify older commits with the “rebase

How to amend a commit on a GitHub Pull Request, When I first tried using git commit --amend I had already pushed to the remote repository and I ended up in some merge conflict trouble when I� # look up the commit id in git log or on github, e.g. 42480f3, then do git checkout master git checkout your_branch git revert 42480f3 # a text editor will open, close it with ctrl+x (editor dependent) git push origin your_branch # or replace origin with your remote

Git Basics: Adding more changes to your last commit, GIT commit, push origin, git commit amend, git push origin, amended commit has So, every now and then, I plan on pushing my local repository to the remote. To complement Jakub's answer, if you have access to the remote git server in ssh, you can go into the git remote directory and set: user@remote$ git config receive.denyNonFastforwards false Then go back to your local repo, try again to do your commit with --force: user@local$ git push origin +master:master --force

Insight: GIT commit amend and already pushed commit. : git, git commit --amend -m "New message"; git push --force repository-name branch- name. Note that using --force is not recommended unless you are absolutely� A git remote command is used to make the remote connections such as connecting a Git local repository with GitHub remote repository. Now, it might look like that git remote is a live exchange of data ( everything you do locally ) between a local and a remote repository, this is not the case.

How to change a Git commit message after a push, Amending a commit after pushing to remote. When using the --amend flag, Git will replace the last commit with the new one commit complete� A Git repository contains the history of a collection of files. We can create a new repository or copy an existing repository. The process of copying an existing Git repository via the Git tool is called cloning. When we clone a repository (stored in a remote location), the complete repository with its history will be copied to our local machine.

Comments
  • What if my --amend was only to change the commit message? Any way to edit the last commit message alone, if it was already pushed to remote? I did that on Github and got the same message about non fast forward. Then I applied a solution below but the merge just added more commit messages on top..
  • @faB: I think that is a FAQ. A commit message is hashed along with the commit, so chaning it changes the revid (hash). If it isn't clear: no you cannot. IIRC can store out-of-band info in notes (so you can annotate existing commits without altering them). To label specific commits, use tags
  • You will soon (git1.8.5, Q4 2013) be able to do a git push -force more carefully.
  • Here is the cowboy style. Don't learn any further or don't hunt ways to undo the previous git amend. Just add some placeholder code, I mean, Add some comment, Cleanup a bit of code or simply added few dash dash dash.... Now make a real commit and push it to remote. Done !
  • @user58777 If your -amend was only to change the commit message and you haven't made any additional local commits since then, you can reset your local branch to the remote commit that you pushed before amending the commit message.
  • I'm very well aware what happens when you force push an amended commit (by destroying history). Luckily enough, I was the only developer on the project with the remote repo being on a network drive so it wasn't that big of a deal. I never thought about merging an amend commit, so I'll upvote this.
  • In our company, we force-push quite regularly... on feature branches developed by individuals.
  • The scolding from Linus was because you erased history with the force option, not because you shouldn't do it. GabrielleV's solution works fine, because it doesn't change history.
  • Please, since the author (gitster) of this answer seems to not exist anymore, could anyone help to clarify the item number 1: find the old commit. If you don't have a backup, where would you find it? Amend and force push wouldn't have it destroyed? Maybe he is referring to get it from a friend/collaborator that still have it in the tree?
  • Dr Breco you can use git reflog to find it