Back

Explore Courses Blog Tutorials Interview Questions
+4 votes
1 view
in DevOps and Agile by (29.3k points)
edited by

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

How can I fix this issue?

1 Answer

+5 votes
by (50.2k points)
edited by

For this, you need to use the following command 

git push origin <your_branch_name> --force

Or if you have a specific repository you can use

git push <git-url> --force

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

--force is having a small flag -f you can use -f also

git push origin <your_branch_name> -f

Reference: https://git-scm.com/docs/git-push#Documentation/git-push.txt---force

For more information about installation of git and common git commands please go through the following link:

Browse Categories

...