How to use Git Revert

后端 未结 5 1091
一生所求
一生所求 2020-12-04 07:51

How is git revert used?

This might sound like a duplicate question but when people ask it, the response is often, use git reset as per Reve

5条回答
  •  感动是毒
    2020-12-04 08:19

    The question is quite old but revert is still confusing people (like me)

    As a beginner, after some trial and error (more errors than trials) I've got an important point:

    • git revert requires the id of the commit you want to remove keeping it into your history

    • git reset requires the commit you want to keep, and will consequentially remove anything after that from history.

    That is, if you use revert with the first commit id, you'll find yourself into an empty directory and an additional commit in history, while with reset your directory will be.. reverted back to the initial commit and your history will get as if the last commit(s) never happened.

    To be even more clear, with a log like this:

    # git log --oneline
    
    cb76ee4 wrong
    01b56c6 test
    2e407ce first commit
    

    Using git revert cb76ee4 will by default bring your files back to 01b56c6 and will add a further commit to your history:

    8d4406b Revert "wrong"
    cb76ee4 wrong
    01b56c6 test
    2e407ce first commit
    

    git reset 01b56c6 will instead bring your files back to 01b56c6 and will clean up any other commit after that from your history :

    01b56c6 test
    2e407ce first commit
    

    I know these are "the basis" but it was quite confusing for me, by running revert on first id ('first commit') I was expecting to find my initial files, it taken a while to understand, that if you need your files back as 'first commit' you need to use the next id.

提交回复
热议问题