How to revert a file to a previous version without overwriting the current changes?

I had a problem trying to revert a file to a previous commit, I know that I can use git checkout to restore a single file, but the problem is that I have changes to this file that I would like to save, so I wondered how to make some kind of "merge" between the previous commit and the current HEAD for one file? I tried using git reset sha-of-my-commit path/to/my/file , but it puts the previous version in the staging area, keeping the latest version in my working directory, not sure how to merge both files after it.

What I was doing at the moment was just git diff ..sha-of-my-commit path/to/my/file and just copy / paste the missing lines, but I think there should be a better way to do this correctly ?

+6
source share
1 answer

Assuming that you mean that the changes are in the working tree (not specified):

 git stash git checkout previous-commit path/to/file git stash pop 

If you made any changes, you can still do it with a little more work. Suppose your story looks like this:

 - x - A - x - x - x - B - x - x (HEAD) 

where you want the version in A, plus the changes from B. Then do the following:

 git stash git checkout B path/to/file git stash git checkout A path/to/file git stash pop git stash pop 

Please note that any firmware application, as it is a mergey operation, can lead to merge conflicts; you must, of course, solve them before moving on!

+4
source

Source: https://habr.com/ru/post/907583/


All Articles