Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git save changes in a 'recovery point' without committing and continue in the same branch?

Tags:

git

I'm working in a new feature and I have made changes in my branch without commiting them because the code is not complete/stable. But then I realise that maybe I can do some refactor to improve a part of the code I have changed, but I'm not sure if the refactor is going to work properly. So I want to save my changes temporally and continue editing the files in the current state. And if the refactorization goes well, everything fine, I can continue coding and when finished all the code, make a commit. But if the refactor doesn't works I want to revert my refactorization changes until the recovery point (but preserving the changes I have made so far).

I don't want to make a git stash because I dont want to undo all my changes, I want to keep working on my changes, and modify only a few lines of that changes. The only way I know until now is to make a commit with a name something like "fake commit to try a refactor", but I think is not the proper way of doing that.

like image 818
Rubén Avatar asked Dec 18 '22 16:12

Rubén


1 Answers

The Git stage is actually a good option for your use case. You may git add all your changed files which you would want to appear in the commit (were to commit right now). Then, just keep working on your test refactor. This would only affect the files in the working directory, but not the stage.

If you decide to keep the changes from your test refactor, then just do another round of git add to any files you have changed. If you decide to discard the changes to the working directory, then use git checkout -- to reset those files.

Your use case is one reason why the Git stage exists.

like image 81
Tim Biegeleisen Avatar answered May 21 '23 04:05

Tim Biegeleisen