20

コミットを元に戻したいのですが、一部のファイルに対してのみです。(チェックアウトではなく、元に戻します。違いに慣れていない場合は、読み続けてください。)

私はこれを試しました

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

そして、私はこのエラーを受け取りました

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

しかし、それはまさに私がしたことです!(そして、はい、my/path/to/revert私の作業ツリーにあります。)

私の作業理論では、一部のファイルのみを元に戻すことは不可能であり、Git エラー メッセージは誤解を招くものであるというものです。

(ギット 1.7.9.5)


これはReverting a single file to a previous version in gitの複製ではありません。

  • その質問は(タイトルにもかかわらず)git-checkoutに関するものです。チェックアウトは、ファイルを以前のバージョンに復元し、その時点以降のすべてのコミットを削除します。
  • 私の質問はgit-revertに関するものです。元に戻すは、特定のコミットで行われた変更を元に戻し、後で行われた可能性のある他のコミットに影響を与えません。そのコミットの逆(のみ)を適用します。
4

4 に答える 4

29

git では特定のファイルを指定して元に戻すことはできないと思います。私が考えることができる最高のものはこれです:

git revert --no-commit <commit hash> # Revert, don't commit it yet
git reset # Unstage everything
git add yourFilesToRevert # Add the file to revert
git commit -m "commit message"
git reset --hard # Undo changes from the part of the revert that we didn't commit
于 2014-04-14T19:49:00.163 に答える
11

必要なものの短いリストを作成できる場合の短いシーケンス:

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
于 2014-04-18T14:12:14.650 に答える