How to force git push after resetting a branch to a previous commit

Hi folks,

In this post I’ll share a really small but handy tip I discovered while using Git.

Let’s say you make some commits on your local repository which you later find undesirable. You can of course use git reset –hard to put the repository in previous state that doesn’t have those commits.

Let’s assume that later (perhaps after adding a few fresh commits) you try to push this to the remote repository, git will refuse by default because the remote repository has more commits.

To force git to accept the push you need to add the -f flag.

git push -f

Note: you should only force a push if you are the only one with access to the repository or you are sure that other users haven’t cloned the existing state of the remote repository, otherwise forcing a push could lead to problems of diverging history for other users.

Leave a Reply

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