Suppose I have five modified files, and I stage two of them for commit (let's call this commit A). Then I still have three uncommitted changes which are left for future commits. Now suppose sometime in the future I decide to revert my working directory to commit A, what would be state of the files that were not staged for commit into A, but were modified nonetheless. Would those changes be lost?
Asked
Active
Viewed 509 times
2
-
1Would be better if you used specific commands in your question instead of using random terminology from other version control systems. I can only guess that you're talking about `git reset` and there you choose explicitly what happens with index and working tree by specifying `--soft`, `--mixed` or `--hard`. – Pavel Šimerda Sep 21 '14 at 15:08
-
2Agreed; "revert" is a precise term in Git but is often used to mean something more like "reset" by those new to the tool. @deepak can you describe what you're trying to do in more detail? – Edward Thomson Sep 21 '14 at 16:03
-
@EdwardThomson Following the comments here, and those to the answer by FractalSpace I think I am beginning to converge to the understanding that what I have been thinking about all along is git reset, rather than revert. – deepak Sep 23 '14 at 13:26
2 Answers
2
A git revert
would only modify the files of the commitA
you are reverting.
It would not touch any other file.
That being said, it can be a good idea to stash those files, just to be sure (git stash
) nothing happen in case of anything "going wrong".

VonC
- 1,262,500
- 529
- 4,410
- 5,250
1
Irrespective of your current staging situation, the 'revert' for commit 'A' will simply create a new commit, (say A*), at the top, which reverses the affect of commit A. You will need to start the revert process with a clean working directory (ie stash or commit your modified/staged files).

FractalSpace
- 5,577
- 3
- 42
- 47
-
1Well, revert works by putting the changes to revert A into the index. So it seems simplistic to suggest that they wouldn't be "involved" without further defining what that actually entails for the staged and unstaged changes. – Edward Thomson Sep 21 '14 at 13:10
-
1Correct. Actually, `git revert` will block if there are any staged or modified files present (edited my answer). – FractalSpace Sep 21 '14 at 15:01
-
So If I understand correctly, my working copy will be changed to reflect the changes in commit A, but the partial work in the other files won't show up? I.e. it is not possible to recover the partial work in other files. This would imply that after reverting, my not working copy is not an exact replication of the working copy earlier. (I do understand that what I am expecting might not be entirely feasible....) – deepak Sep 21 '14 at 15:38
-
1@deepak Reading your post again, ".. Now suppose sometime in the future I decide to revert my working directory to commit A ..." suggests you may be confusing `git revert` with `svn revert`. If that is correct, you want to do a `git reset commit-A`, which brings your working copy back to the state of commit-A. `git revert commit-A` on the other hand does not actually bring your "working directory to commit A". It simply reverts the affects of commit-A, keeping all other files and follow-up commits as is. – FractalSpace Sep 21 '14 at 17:15
-
1Generally speaking, if you want to preserve uncommitted 'partial' (staged/modified files) work, then use `git stash` to temporarily save those files out of git's visibility (followed by `git stash pop` when need to bring those back). – FractalSpace Sep 21 '14 at 17:15
-
@FractalSpace It will not block in the general case: you're welcome to revert when you have changes staged, as long as you do not have a change staged to a file that will be affected by the revert. – Edward Thomson Sep 21 '14 at 20:42
-
@EdwardThomson If you have mix of modified/staged (all uncommitted) files, `git revert` will fail with: `error: Your local changes would be overwritten by revert. hint: Commit your changes or stash them to proceed. fatal: revert failed` – FractalSpace Sep 21 '14 at 22:49
-
@FractalSpace Yes, if the revert would affect one of those modifications. If you modify file `A` and the revert only affects file `B`, the revert will stage file `B` and file `A` is still modified. – Edward Thomson Sep 21 '14 at 22:54