If you are sure that neither a soft reset, nor the creation of several branches are suitable for your use case, you can do
git diff HEAD commit_hash_to_go_to | git apply
This will create the difference between the last commit in your branch and the commit with the desired state and automatically apply it. It will simply change the files, it is your job to add them to the production and record the result. This can be useful if you want to try out different solutions and keep a history of changes in the same branch or avoid multiplying local branches.
If you encounter the error "it is impossible to apply binary correction without a full index line", add the --binary flag:
git diff HEAD commit_hash_to_go_to --binary | git apply
Before doing this, make sure that you do not have uncommitted changes - otherwise the patch will not be applied (it is atomic, so either all changes go through or not, so you will not end up in an inconsistent state)
NOTE: this simply modifies the files and marks them as modified. This does NOT change the history of commits and does not create new commits.
source share