Revert changes to a file in a commit

后端 未结 8 1946
無奈伤痛
無奈伤痛 2020-12-12 09:45

I want to revert changes made by a particular commit to a given file only.

Can I use git revert command for that?

Any other simple way to do it?

8条回答
  •  鱼传尺愫
    2020-12-12 10:05

    Assuming it is ok to change the commit history, here's a workflow to revert changes in a single file in an earlier commit:

    For example, you want to revert changes in 1 file (badfile.txt) in commit aaa222:

    aaa333 Good commit
    aaa222 Problem commit containing badfile.txt
    aaa111 Base commit
    

    Rebase on the base commit, amend the problem commit, & continue.

    1) Start interactive rebase:

    git rebase -i aaa111
    

    2) Mark the problem commit for edit in the editor by changing pick to e (for edit):

    e aaa222
    pick aaa333
    

    3) Revert changes to the bad file:

    git show -- badfile.txt | git apply -R
    

    4) Add the changes & amend the commit:

    git add badfile.txt
    git commit --amend
    

    5) Finish the rebase:

    git rebase --continue
    

提交回复
热议问题