Git revert certain files

前端 未结 3 1152
不思量自难忘°
不思量自难忘° 2020-12-16 13:26

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 thi

相关标签:
3条回答
  • 2020-12-16 13:42

    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
    
    0 讨论(0)
  • 2020-12-16 13:42

    I don't think git lets you specify particular files to revert. The best I can think of is this:

    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
    
    0 讨论(0)
  • 2020-12-16 13:54

    vcsjones' answer is probably the best way since revert uses the three-way merge machinery. However, for many cases, you could git apply -R (reverse) the patch to the file (or files) in question, e.g.:

    git show <rev> -- path/to/file | git apply -R
    

    (or git diff—any diff generator that allow you to limit the result to specific files, really—but git show is the obvious go-to for non-merge commits; for merges you'd need to specify the correct parent).

    0 讨论(0)
提交回复
热议问题