Skip to main content

Command Palette

Search for a command to run...

Rewinding a remote branch [GIT]

Published
1 min readView as Markdown
Rewinding a remote branch [GIT]
A

Developer and Builder 🔨⚒️⚙️

You were working in a feature branch but accidentally pushed commits to the master/develop branch, it happens.

First, you have to know the exact commit ID or number of commits that was pushed. You can use git log pretty=oneline to get the information.

If you don't have an exact copy of the branch you would love to revert and would love to keep the commit history, then create a dummy branch that will hold the exact commit logs, ensure it branches from the branch with the original changes git checkout -b {dummy-branch-name}

Next, return back to the branch you pushed to accidentally with git checkout {branch-name} and run git reset {commit-id} --hard if you have a valid commit ID or git reset HEAD^{commit-count} if you have a commit count you want to revert back to.

The about command would rewind all the changes.

Next, do git stash to put the changes aside in a trash can, ensure that the head of the current branch is at the exact commit you want, then run git push --force. This would force a rewrite in the remote branch and revert the changes as you had done locally.

I hope this takes someone from a distress!!!

Gracias