Home  >  Q&A  >  body text

版本控制 - git 如何还原某个文件

目的

想把文件 include/plugin/funclib/function_member.php 恢复到 b257f8685b71a471c4f797d05f52a1a576047ed2 (2014-12-29 23:38)这个版本,但是使用 git reset b257f8685b71a471c4f797d05f52a1a576047ed2 include/plugin/funclib/function_member.php 一直无法实现。

代码


Administrator@USER-20140115FZ /D/website/xxx.com/www (master) $ git log include/plugin/funclib/function_member.php commit bbb0a005d245e6d5ef5b1c0b1b0ef2981fa3ecb9 Author: ken <xxx@gmail.com> Date: Mon Jan 5 22:07:37 2015 +0800 for reset commit b257f8685b71a471c4f797d05f52a1a576047ed2 Author: ken <xxx@gmail.com> Date: Mon Dec 29 23:38:09 2014 +0800 add reg forbbiden commit 052c0233bcaef35bbf6e6ebd43bfd6a648e3d93b Author: ken <xxx@gmail.com> Date: Mon Dec 29 21:51:44 2014 +0800 add coname filter for register commit 53298e00de944863f6092d0d4c0a260d00124f9f Author: ken <xxx@gmail.com> Date: Mon Dec 29 01:24:13 2014 +0800 add referrals.php delete some php files like serv.php yqm.php,modify registe commit 44118e5256eb9786d024f9b7d2dfb8af0bd8a308 Author: default7 <xxx@gmail.com> Date: Sun Jul 27 17:05:21 2014 +0800 first commit Administrator@USER-20140115FZ /D/website/xxx.com/www (master) $ git reset b257f8685b71a471c4f797d05f52a1a576047ed2 include/plugin/funclib/fun ction_member.php --hard fatal: Cannot do hard reset with paths. Administrator@USER-20140115FZ /D/website/xxx.com/www (master) $ git reset b257f8685b71a471c4f797d05f52a1a576047ed2 include/plugin/funclib/fun ction_member.php Unstaged changes after reset: M include/plugin/funclib/function_member.php Administrator@USER-20140115FZ /D/website/xxx.com/www (master) $
过去多啦不再A梦过去多啦不再A梦2731 days ago822

reply all(6)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-04-28 09:08:04

    Just use git-checkout directly. It's just a little strange to understand.

    $ git checkout ${commit} /path/to/file
    

    Source reference:

    • http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git
    • https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html

    reply
    0
  • 滿天的星座

    滿天的星座2017-04-28 09:08:04

    Still needs to be executedgit checkout xxxxx(文件名)

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-04-28 09:08:04

    Git cannot restore a single file to a specified version. If you want to do this, you can do this
    1. git reset version number. At this time, a branch corresponding to the version number will be generated
    2. git checkout to this branch and copy the files to be restored
    3. git checkout to the original branch, copy the file back, overwrite the current file, then git add, git commit

    reply
    0
  • 世界只因有你

    世界只因有你2017-04-28 09:08:04

    I think it’s good to use tortoisegit. After configuration, there is a graphical interface directly, and you can use rever to restore the previous version

    reply
    0
  • 漂亮男人

    漂亮男人2017-04-28 09:08:04

    1. If your submission has not been shared and published to the remote end, you can use git rebase -i origin/branch to edit the corresponding branch and then resubmit git commit --ammend.
    2. If your submission has been shared to the remote end, you can only use git revert to the corresponding branch, then checkout other files, keep only this modification, and then submit.
    3. It’s a bit complicated. It would be very convenient if there is a gui tool. I usually click on a file of a certain version in the git control of webstrom, right-click revert and then submit it

    reply
    0
  • PHP中文网

    PHP中文网2017-04-28 09:08:04

    git checkout <sha1-of-a-commit> </path/to/your/file>

    reply
    0
  • Cancelreply