How to undo a git push to a remote repository.
Sometimes while working on a project, you may often get carried away. For Instance, forgetting to create a new repository locally for a separate project after cloning it or committing to a wrong branch.
It becomes a problem when you suddenly realize your error but have already pushed it to the wrong remote branch or repository. This can cause some issues for you, especially if you are working with other people on the same project.
Ideally, this is not what you want but it can be encountered sometimes. So you want a way to quickly reverse these changes on the remote branch/repository to their original state.
There is a simple command you can use to quickly revert these changes on your remote repository.
Undoing commits already pushed to the remote repository.
Copy the SHA-1 checksum of the last known stable commit you want to revert back to.
Ensure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits you want to remove because you are about to rewind history.
Then you need to 'force' push the old reference.
git push -f origin <last_known_stable_commit>:branch_name
You can reference this StackOverflow answer here.
Check if the old remote repository is still attached and remove it.
View current remotes -
git remote -v
Copy the remote destination URL if it exists.
Remove the remote destination -
git remote rm <remote_destination>
Verify that it is gone -
git remote -v
You can reference this article on GitHub here.
Then, create your own branch or repository and push your changes.😊
Hope you found this article succinct and helpful. Thanks for reading!