在git commit之后,是否可以只恢复一个文件?

时间:2014-07-30 13:22:28

标签: git github

我有两个提交合并为一个具有特定更改ID的提交

commit fea9c375f216a75c6a2a2fcc9c903a5992d3679e
    adding Field
    Change-Id: Id07dd67b412d56f64e05246ea0b87924f8a803cb

现在我已经提交了5个文件..

我必须在此提交中仅还原一个文件。有可能吗?

git reflog
fea9c37 HEAD@{0}: reset: moving to ORIG_HEAD

6bf010f HEAD@{1}: rebase -i (finish): returning to refs/heads/dev

6bf010f HEAD@{2}: rebase -i (pick): adding Field

2a0b1d7 HEAD@{3}: rebase -i (start): checkout remotes/gerrit/dev

fea9c37 HEAD@{4}: rebase -i (finish): returning to refs/heads/dev

fea9c37 HEAD@{5}: rebase -i (squash): adding Field

e2f9b47 HEAD@{6}: rebase -i (start): checkout refs/remotes/origin/dev

27cc4f3 HEAD@{7}: checkout: moving from bf4a4564ed3ddcb1de1114f2d020d65ead2e9999 to dev

bf4a456 HEAD@{8}: rebase -i (start): checkout refs/remotes/origin/dev

27cc4f3 HEAD@{9}: commit: printField

e2f9b47 HEAD@{10}: commit (amend): adding Field

afddac7 HEAD@{11}: reset: moving to ORIG_HEAD

a614916 HEAD@{12}: rebase -i (finish): returning to refs/heads/dev

a614916 HEAD@{13}: rebase -i (pick): adding Field

c766b57 HEAD@{14}: rebase -i (start): checkout remotes/gerrit/dev

afddac7 HEAD@{15}: commit: adding Field

bf4a456 HEAD@{16}: pull: Fast-forward

5d63ab1 HEAD@{17}: checkout: moving from master to dev

我试过了 git reset fea9c37 <filename>

但更改仍然存在于文件中。

我如何还原这个?

1 个答案:

答案 0 :(得分:2)

您可以使用git checkout将文件还原为之前的提交。

例如为: git checkout Id07dd67b412d56f64e05246ea0b87924f8a803cb path/to/file

(其中Id07d...是您要将文件重置为 - 在更改之前的提交)