-1

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 .)?

YulePale
  • 6,688
  • 16
  • 46
  • 95

1 Answers1

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Docs are here: https://git-scm.com/docs/git-restore – matt Oct 16 '21 at 15:19
  • Thank you for your explanation. I do not understand why someone downvoted my question. Anyway, I am still confused. From this [answer](https://stackoverflow.com/a/60005636/9953550) they are saying `git-restore is a tool to revert non-commited changes. Non-commited changes are: a) changes in your working copy, or b) content in your index (a.k.a. staging area).` I think what I was asking is how to revert to a previous version if the changes have been commited. If I am reverting to a previous commit, can I revert just for one file even though the commit contained several files? – YulePale Oct 16 '21 at 15:30
  • Well, I really don't care what some other answer says. My answer says what _my_ answer says, and if you don't believe it, read the docs. I've answered the question and I stand by my answer. Try doing what I said. – matt Oct 16 '21 at 15:45
  • I understand what you are saying. Thank you for your help. The reason I linked that answer is because I easily understand what they are saying. I am honestly still a bit confused. I find the docs to be a bit cryptic that is why I ask here. – YulePale Oct 16 '21 at 15:56
  • 1
    Well, they can certainly be cryptic. Nevertheless it's worth learning to read them. The docs say of `git restore`: "Restore specified paths in the working tree with some contents from a restore source... `--source=` Restore the working tree files with the content from the given tree. It is common to specify the source tree by naming a commit." So this tells you that restoring a file from a commit is _exactly_ the kind of thing this command can do. – matt Oct 16 '21 at 16:28