Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

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.)

1 Answer

0 votes
by (50.2k points)

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.

Merge that to your master with git merge master

Update your master with the result with git push . HEAD:master

Push the result out.

So their later merges will not see the conflicts between old and new that resulted from your amending.

Browse Categories

...