Skip to content
Fix Code Error

How do I properly force a Git push?

March 13, 2021 by Code Error
Posted By: Anonymous

I’ve set up a remote non-bare “main” repo and cloned it to my computer. I made some local changes, updated my local repository, and pushed the changes back to my remote repo. Things were fine up to that point.

Now, I had to change something in the remote repo. Then I changed something in my local repo. I realized that the change to the remote repo was not needed. So I tried to git push from my local repo to my remote repo, but I got an error like:

To prevent you from losing history, non-fast-forward updates were
rejected Merge the remote changes before pushing again. See the ‘Note
about fast-forwards’ section of git push --help for details.

I thought that probably a

git push --force

would force my local copy to push changes to the remote one and make it the same. It does force the update, but when I go back to the remote repo and make a commit, I notice that the files contain outdated changes (ones that the main remote repo previously had).

As I mentioned in the comments to one of the answers:

[I] tried forcing, but when going back to master server to save the changes, i get outdated staging. Thus, when i commit the repositories are not the same. And when i try to use git push again, i get the same error.

How can I fix this issue?

Solution

Just do:

git push origin <your_branch_name> --force

or if you have a specific repo:

git push https://git.... --force

This will delete your previous commit(s) and push your current one.

It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution…

Short flag

Also note that -f is short for --force, so

git push origin <your_branch_name> -f

will also work.

Answered By: Anonymous

Related Articles

  • Why do I have to "git push --set-upstream origin "?
  • Git - Pushing code to two remotes
  • Why do git fetch origin and git fetch : behave differently?
  • Git Using Remote Branch
  • Why does git perform fast-forward merges by default?
  • How to track untracked content?
  • Checkout another branch when there are uncommitted changes…
  • Why call git branch --unset-upstream to fixup?
  • Ubuntu apt-get unable to fetch packages
  • “tag already exists in the remote" error after recreating…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Get class name using jQuery

Next Post:

SQL Inner-join with 3 tables?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error