开发者

Git's equivalent of CVS's checkout or update?

开发者 https://www.devze.com 2023-03-18 15:35 出处:网络
OK, so I added and committed an entire subdirectory to git (no push yet, still local, to .git). I then deleted the entire subdirectory. (.git is still there.)

OK, so I added and committed an entire subdirectory to git (no push yet, still local, to .git).

I then deleted the entire subdirectory. (.git is still there.)

I now want it back, from git. How do I do that?

I tried git checkout but it only lists the 开发者_StackOverflow中文版delete files in that subdirectory.

What am I missing?


You have to do git checkout subdirname


You can also do git checkout -- subdirname

The -- is used to avoid ambiguity ( like say if you had a branch that was named subdirname and other corner cases.) Below is a snippet from man for git checkout:

If you have an unfortunate branch that is named hello.c, this step would be confused as an instruction to switch to that branch. You should instead write:

$ git checkout -- hello.c

http://git-scm.com/docs/git-checkout


You need to perform git checkout -- file_path to restore it. Note the git checkout -- a_file_name syntax lets you retrieve not only deleted files/dirs, but their original (last committed) form before any modification. So if you wanted to undo any local modifications to a_changed_file, git checkout -- a_changed_file will work for this use case as well.

-- is recommended to avoid problems with bad filenames, such as those starting with a hyphen. It also removes ambiguity in the case of a branch and a file sharing the same name.

The git community book's chapter on undoing is a good reference for related questions.

0

精彩评论

暂无评论...
验证码 换一张
取 消