简体   繁体   中英

How to remove commits from GIT origin?

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM