How to use Git Revert

后端 未结 5 1092
一生所求
一生所求 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:32

    The reason reset and revert tend to come up a lot in the same conversations is because different version control systems use them to mean different things.

    In particular, people who are used to SVN or P4 who want to throw away uncommitted changes to a file will often reach for revert before being told that they actually want reset.

    Similarly, the revert equivalent in other VCSes is often called rollback or something similar - but "rollback" can also mean "I want to completely discard the last few commits", which is appropriate for reset but not revert. So, there's a lot of confusion where people know what they want to do, but aren't clear on which command they should be using for it.

    As for your actual questions about revert...

    Okay, you're going to use git revert but how?

    git revert first-bad-commit..last-bad-commit

    And after running git revert do you have to do something else after? Do you have to commit the changes revert made or does revert directly commit to the repo or what??

    By default, git revert prompts you for a commit message and then commits the results. This can be overridden. I quote the man page:

    --edit

    With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal.

    --no-commit

    Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

    This is useful when reverting more than one commits' effect to your index in a row.

    In particular, by default it creates a new commit for each commit you're reverting. You can use revert --no-commit to create changes reverting all of them without committing those changes as individual commits, then commit at your leisure.

提交回复
热议问题