简体   繁体   中英

How to undo git commits locally and on remote without losing changes

We have a project in remote repository. Recently I created a new branch "Feature7" and made huge changes and committed twice and also pushed twice. Fortunately, no one pulled from that branch. After that there have been lots of commit and push in master branch but no one worked on Feature7 branch.

Now I want to undo the two commits and push, both from local and remote if possible of the Feature7 branch. But I want the changes to keep locally as well. I don't want to loose my changes.

So in short, I only want to undo both commits and push without losing my changes in local.

The reason I am doing this because, in the new branch there is a problem for line endings of whole project. So I messed up the history of the whole project. It is not possible to recognize what changes I made in my new branch.

I would first just pull your feature7 branch to a local backup branch:

git checkout -b feature7-backup origin/Feature7

Then go to your original feature7 branch, reset the previous two commits and then force push to your remote:

git checkout feature7
git reset HEAD~2
git push -f origin HEAD:Feature7

All of this assumes your remote is origin

Now the remote no longer has those two commits, but you have a copy of them locally in the "feature7-backup" branch (as well as uncommitted on your current branch after the reset).

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