gitgit-reset

Reset all files affected by past commit to the state before that commit


How do I reset all (and only) files affected by a given commit to the state before that commit?

I only need to reset the files in the commit, not the entire repository or single file/directory, and I don't want to have to type explicitly each file name.

There have been other commits after this one changing some of the files, so I can't just revert the commit due to conflicts with later commits.

EDIT By "can't just revert" I meant that I tried it to see how far it takes me but failed at the very start.


Solution

  • git checkout $that_commit^ -- $(git diff --name-only $that_commit^ $that_commit)
    

    And now your current checkout will have the that commit's parent's versions of the files that differ in that commit from its parent, exactly as requested. If you've got white space in your file names you'll want to shut off some of the shell's word splitting,

    ( IFS=$'\n'
      git checkout $that_commit^ -- $(git diff --name-only $that_commit^ $that_commit)
    )
    

    so it only takes newlines as separators in that command's expansions.