Git: checking out a file from a previous commit and amending it to HEAD

前端 未结 2 480
清歌不尽
清歌不尽 2021-01-30 15:47

I recently committed a file to the HEAD of my branch which has errors in it. I need to do the following things:

  • Get that file from one commit previous to HEAD <
2条回答
  •  情话喂你
    2021-01-30 16:45

    You've practically said it yourself:

    First get the file back from one commit before:

    $> git checkout HEAD~1 path/to/file.ext
    

    Then commit it:

    $> git commit -a -m 'Retrieved file from older revision'
    

    If only the changes to that file where present in the last commit, you can even use git-revert:

    $> git revert HEAD
    

    I think it would be better to make this a separate commit, because it tells you exactly what you've reverted, and why. However, you can squash this into the previous commit by using the --amend switch to git-commit.

提交回复
热议问题