Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly force a Git push?

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?

like image 856
Spyros Avatar asked Apr 01 '11 05:04

Spyros


People also ask

Should you force push git?

If you want to prioritize your local branch over the remote one, overwriting any commits made along the way, then by all means go for it. In a less extreme situation, Force Pushing can still be appropriate. Sometimes we're simply working on a private branch that should not be used by anyone else.

Is force push a good practice?

Force-pushing is a highly threatening and risky method if you're working in a common repository. Using this force pushing you should be able to force your local revision to the remote repository. But forcefully pushing to remote repo is not a good practice.

How do I enable force push on GitHub?

As shown in the image below, select Allow force pushes and Specify who can force push. Then, search for and select the people and teams who should be allowed to force push. For more information, visit Managing a branch protection rule.

What is the problem with git force -- push?

git push --force overwrites the remote branch, while git push --force-with-lease only overwrites the remote branch if your local copy is aware of all of the commits on the remote branch. This difference makes it significantly more difficult to destroy someone else's changes on the project.


1 Answers

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.

like image 87
Katie Avatar answered Oct 14 '22 05:10

Katie