Search code examples
gitvisual-studiotfvc

What is the equivalent of TFVC "Rollback" in Git?


If I use Team Foundation Source Control I can easily rollback to old changeset.

Assuming I have a file with 4 versions, each version have a changset:

  • Changeset a126 - Version 1.
  • Changeset b347 - Version 2.
  • Changeset c560 - Version 3.
  • Changeset d912 - Version 4.

Now I found a lot of bugs in Versions 3 & 4 and I want quickly return back to Version 2.

In Visual Studio I can click "View History" on the file, click on Changeset b347 ("Version 2") and then "Rollback".

Now I have a new changeset with the file in Version 2 and I can check-in it (I still have Version 3/4 in my history, so I can also return to them sometime).

In Git, I know there are revert, reset (hard, soft) but I don't know exactly what I need to do to reach the same result.

So what is the best way to rollback to old version in Git (preferred via Visual Studio)?


Solution

  • Suppose you have 4 commits A-B-C-D, the file foo.txt is changed in each of them, and the codes of C and D have bugs introduced by foo.txt.

    1.Other files may be changed in C and D. If you want to rollback only foo.txt to the version of B:

    git checkout B -- foo.txt
    

    If you want to commit the rollback,

    git commit
    

    And the history will be A-B-C-D-R1.

    2.If you want to rollback all the changes of C and D (including those of other files if any), and CD have been pushed to the remote repository:

    git revert D C
    

    And the history will be A-B-C-D-R2-R3.

    3.If you want to rollback all the changes of C and D, and CD have NOT been pushed to the remote repository:

    git reset B --hard
    

    The history will be A-B. You could also use this solution for Case 2, but you then need to force-push the branch to overwrite the branch in the remote repository. If other contributors have fetched the old history, you need to tell them to fetch the new history.