How to discard committed changes to just one file?

This question already has an answer here:

  • Reset or revert a specific file to a specific revision using Git? 28 answers

  • Checkout checks out that file in that commit.

    When you do git checkout -- filename , that -- just says "end of options", meaning whatever comes after that mark is an argument, not an option.

    In this case, the ref HEAD is used if a commit is not specified, so the file as in HEAD is taken and applied to the current commit's file(which means nothing really happens, just the changes that have not been committed disappear; if the directory is clean, nothing happens at all).

    If a commit ID is specified, then the file at that commit is applied to the current file.

    Example
    Suppose you have a commit from 5 days ago that has an ID of 257d2c7 .
    In this commit you added a verse to a poem you were writing(who says git only helps with source code??) which you then decided is not that good and removed it.
    Then you made a couple more commits on top of that. But now you want that particular verse back, how'd you do it?

    Like follows:

    $ git stash // save existing changes and clean working directory
    $ git checkout 257d2c7 my-poem.txt // check out your poem from 5 days ago
    $ git stash pop // reapply existing changes and resolve conflicts
    $ ... // continue working
    
    链接地址: http://www.djcxy.com/p/8980.html

    上一篇: 硬重置单个文件

    下一篇: 如何丢弃对一个文件的提交更改?