So a long time ago I added a folder abc with a group of files in it to my project and made a commit with revision xyz. After many commits someone has erased the folder with git rm -r abc. After this there has been a lot of comm开发者_StackOverflow社区its as well.
Now I need to get back the folder abc. I do
git revert xyz
After this I can find folder xyz but not all of the files that were added in the commit xyz are present.
What am I doing wrong? I dont want to do reset because I want to keep the history between commit xyz and the present.
What you really want is this:
git checkout xyz -- abc
That says "get me the version of the stuff at path abc
, as it was in commit xyz
". That way, you'll get your folder back in the state you committed it, but you won't touch any other files that may have been modified since.
(The --
isn't necessary if you're explicitly specifying the commit, unless the name of the commit happens to also be a filename in your project. For instance, if you're trying to check out the version of a file named HEAD
from the commit pointed to by master
, you'd need to do git checkout master -- HEAD
. [Just save yourself the time and don't name your branches and tags the same things as your files.])
You don't want git revert xyz
because that's for creating commits that reverse other commits, and you obviously don't want to reverse your adding the files in the first place.
git revert xyz
does not bring your code back to the state in which it was at revision xyz
- it creates a new commit that undoes whatever was done in xyz
. You could try git cherry-pick --no-commit xyz
, which will replay the changes that were done in xyz
(if you leave out --no-commit
, it will automatically become a new commit, but maybe you don't want everything that was done in xyz
).
精彩评论