Home > Software design >  Can I revert to a previous version of just one file even if I git added all files before a commit?
Can I revert to a previous version of just one file even if I git added all files before a commit?

Time:10-17

Can I revert to a previous version of just one file even if I git added all files before a commit?

So, this is what I did: git add . then git commit -m "my changes" then git push

So there have been many other git add . and commits between "my changes" and the current head.

Is it possible to revert back to "my changes" state for only one file even though I git added several files(git add .)?

CodePudding user response:

The entire worry in your question is based on a total misconception of what Git is.

Every commit contains all the files. They are all snapshots of the entire project. git add just tells Git what files from the working tree to notice in particular for the next commit, but whether you include a file explicitly by saying git add or not before a particular commit, if that file is in a previous commit, it is in every commit thereafter.

So your saying git add . is totally irrelevant to anything.

The question becomes simply whether you can restore the state of a file. And the answer is yes, of course; every commit contains all the files, so just find a commit where you like the state of that particular file and extract it:

git restore --source=<thatCommitSHA> -- thatFile

Now git add that file (and anything else you feel like) and commit.

  • Related