Git恢复某些文件 [英] Git revert certain files

查看:260
本文介绍了Git恢复某些文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想恢复提交,但仅限于某些文件。 (不是结账;还原。)如果您不熟悉这种差异,请继续阅读。)



我试过这个

  git revert --no-commit abcdef123456  -  my / path / to / revert 

我得到这个错误

 致命:ambiguous argument'my / path / to / revert':未知版本或路径不在工作树中。 
使用' - '来分隔修订版本的路径

但是,这正是我所做的! (是的, my / path / to / revert 位于我的工作树中。)我的工作理论是(Git 1.7.9.5)
$
$ b

(git 1.7.9.5)

b $ b


这不是 -single-file-to-a-before-version-in-git>在git中将单个文件还原为以前的版本


  • 该问题(尽管标题)与 git-checkout 有关。结账将文件恢复到以前的版本,并在此之后删除所有提交。 我的问题与 git-revert 有关。撤消撤销对特定提交中所做的更改,而不会触及可能稍后提交的其他提交。它适用于(仅)该提交的反向操作。


解决方案

可以做一个你想要的短列表:

  git revert that_commit#做整个还原
git reset - 硬HEAD ^#事实证明是一次性提交
git checkout HEAD @ {1} - 一个/文件夹#只需要你想要的结果


I want to do a revert of a commit, but only for some files. (Not a checkout; a revert. If you are unfamiliar with the difference, keep reading.)

I tried this

git revert --no-commit abcdef123456 -- my/path/to/revert

And I got this error

fatal: ambiguous argument 'my/path/to/revert': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

But that is precisely what I did! (And yes, my/path/to/revert is in my working tree.)

My working theory is that it is not possible to revert only some files, and that the Git error message is misleading.

(Git 1.7.9.5)


This is not a duplicate of Reverting a single file to a previous version in git.

  • That question (despite the title) pertains to git-checkout. A checkout restores a file to a previous version, removing all commits after that point.
  • My question pertains to git-revert. A revert undoes changes made in a particular commit, without touching other commits that may have come later. It applies the reverse of (only) that commit.

解决方案

A shorter sequence for when you can make a short list of what you want:

git revert that_commit           # do the whole revert
git reset --hard HEAD^           # in what turns out to have been a throwaway commit
git checkout HEAD@{1} -- one/folder   # and just take what you want of the results

这篇关于Git恢复某些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆