Git - How to revert, cancel last changes on remote server
Sometimes you do something wrong. For example, push to master branch dev commits or something else, what does not have to find itself here.
There are present a lot of official recommends how to push revert commits... but honestly, in my case, it does not work in proper way, as you want.
So, I found ferroconcrete method how to solve this issue. But it seems to somebody not gracefully, but it works at least.
First step:
Revert to required commit.
git reset --hard <commit-hash>
(This command will reset the branch to the specified commit. Be cautious when using --hard as it will discard changes in your working directory and staging area.)
Second step:
Create new branch from this point.
git checkout -b <new-branch-name>
(This command creates a new branch and switches to it. Replace <new-branch-name> with the desired name for your new branch.)
Third step:
Remove your damaged branch on server.
git push origin --delete <branch-name>
(Replace <branch-name> with the name of the branch you want to remove on the server. This command deletes the specified branch both locally and remotely.
Make sure you are not on the branch you want to delete before running this command. If you are on the branch, you can switch to another branch using git checkout or git switch before executing the git push command.)
Fourth step:
Rename your new branch to old one and push it to server.
git branch -m <new-branch-name>
(This command renames the current branch to the new name.)
git push origin -u <new-branch-name>
(This command pushes the local branch changes to the remote repository and sets the upstream branch.)
Voila! It fixed!
Enjoy to use this method.
Comments