2

I have two remotes for one of my git repositories.

1. origin
2. new-origin

I always used to push my commits to the new-origin remote. But today I accidentally pushed my commits to the origin remote. So is there any way to revert these commits from the origin remote?.

I have searched and tried a lot of ways but am only getting ways to remove pushed commits. I am scared if I remove those commits by git reset it might affect my new-origin also.

trent
  • 25,033
  • 7
  • 51
  • 90
Ravi Kumar SIngh
  • 311
  • 1
  • 4
  • 17
  • have you pushed to new-origin also, if not push it there, then use git reset hard that will remove commits from your local repository then push to origin ( you need to do force push ) – Abhay Sehgal Aug 12 '19 at 15:44
  • Possible duplicate of [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – Alexpandiyan Chokkan Aug 13 '19 at 05:46

2 Answers2

3

As I've mentioned in the comment, firstly push commits to new-origin so your new-origin repository will get updated, then reset your local repository by

git reset head~<no of commits here start from 0> --hard

After resetting, your head will be at your desired commit. Then push it to origin. You need to force push it:

git push origin <branch-name> --force

Now your origin repo will be at the desired commit, and then you can update your local repository by pulling from new-origin:

git pull new-origin <branch-name>
aschultz
  • 1,658
  • 3
  • 20
  • 30
Abhay Sehgal
  • 1,603
  • 2
  • 15
  • 21
0

suppose your history looks like this:

22222 new commit pushed accidentally to new-origin
11111 last commit that should have been on new-origin
00000 older commits

then you can:

git push --force new-origin 111111:<branchname>

NOTE: --force is a dangerous option: you are removing commits from new-origin... work may be lost if you don't pay attention

Chris Maes
  • 35,025
  • 12
  • 111
  • 136