Can I do a partial revert in GIT

后端 未结 4 1000
迷失自我
迷失自我 2020-12-22 15:34

Is it possible to revert only a single file or certain changes in a file in multi file commit?

Full story I committed a bunch of files. A number of

4条回答
  •  旧巷少年郎
    2020-12-22 16:27

    I found a way to do this on the Git mailing list:

    git show  --  | git apply --reverse
    

    Source: http://git.661346.n2.nabble.com/Revert-a-single-commit-in-a-single-file-td6064050.html#a6064406

    Variations

    That command fails (causing no changes) if the patch does not apply cleanly, but with --3way you instead get conflicts which you can then resolve manually (in this case Casey's answer might be more practical):

    git show  --  | git apply --reverse --3way
    

    You can also use this to partially revert multiple commits, e.g.:

    git log -S --patch | git apply --reverse
    

    to revert files with changes matching in any commit. This is exactly what I needed in my use case (a few separate commits introduced similar changes to different files, along with changes other files in unrelated ways that I did not want to revert).

    If you have diff.noprefix=true set in your ~/.gitconfig then you need to add -p0 to the git apply command, e.g.

    git show  --  | git apply -p0 --reverse
    

提交回复
热议问题