Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert a commit without losing the changes?

Tags:

git

I'm a confused about all the various mechanisms for undoing commit but not undoing the changes themselves..

In my situation, my commit history is something like this.

original commit

dataclass added

controllerclass added

controllerclass and dataclass fixes

What I want to get back to is the state I was in immediately prior to executing the commit -m 'controllerclass and dataclass fixes'and git push origin master.
commands. That is, I want the source to remain as it is, but have a bunch of uncommitted local changes subsequent to the controllerclass added commit, but the current source is unchanged.

I think what i want to do is git revert HEAD, but I'm not sure if revert will just set my code to the state of 'controller class added` and all the subsequent changes I made will be lost.

like image 245
GGizmos Avatar asked Aug 10 '26 02:08

GGizmos


1 Answers

git revert will create a new commit that reverts the changes you've made to your repository in the given commit; it will not change any of your current commits. This is useful in projects where you don't want to roll back history, but a particular change is broken or needs to be done differently.

What it sounds like you want is git reset --soft HEAD^. That will rewind your last commit to be the “controllerclass added” commit and then leave the changes from your latest commit staged for commit. If you don't want the changes staged, use git reset --mixed HEAD^ instead.

Note that this rewinds history, so if you've already pushed your changes, you'll need to force-push to the server by putting a plus sign (+) before the branch name (e.g., git push origin +master). If you're working on a project with other people and have already pushed, then it's probably better to just admit you made a mistake and use git revert instead.

like image 76
bk2204 Avatar answered Aug 13 '26 02:08

bk2204