I have many branches
on my repo. I accidentally pushed to branch named feature/customer
. Now I want to roll back that push. How can I rollback that push on branch feature/customer
in git?
-
Do you want to remove the branch you pushed to the remote? – nitishagar Apr 16 '20 at 06:42
-
No, this branch has lots of code already. I just pushed some wrong code on this branch. Now i want to revert only my last push on this branch – ahsan Apr 16 '20 at 06:43
-
Check this: https://stackoverflow.com/questions/1270514/undoing-a-git-push – nitishagar Apr 16 '20 at 06:47
2 Answers
Assuming that the feature/customer
branch is shared by other people, and that someone else may have already pulled your accidental commit, the best thing to do would be to revert the commit:
# from feature/customer
git revert HEAD
This will add a new commit on top of the customer
branch which will functionally undo whatever your accidental commit may have introduced. Now you only need to do a regular push again to get this revert commit to the repository.
Note that if the accidental commit contains any sensitive information, you could try to overwrite the remote branch. However, there would always still be the possibility that someone else already pulled. In addition, the previous accidental commit would still be present in the remote reflog for some time.

- 502,043
- 27
- 286
- 360
-
-
@ahsan Comments in Git start with `#` ... this is just a comment – Tim Biegeleisen Apr 16 '20 at 06:56
-
so i have to stand on branch `feature/customer` and run this command `# from feature/customer`? and what will do this command? will it undo my last push on this branch? – ahsan Apr 16 '20 at 06:57
-
-
If you have permissions on the remote. You can do the following:
git push -f origin last_known_good_commit:branch_name
Also, you can refer to Undoing a 'git push'

- 9,038
- 3
- 28
- 40