0

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)?

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114

5 Answers5

2

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.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Great answer! thank you! I don't think I can revert 2 commits in VS in one revert, but I can do it in Command Line :) – Shayki Abramczyk Apr 11 '19 at 07:36
  • @ShaykiAbramczyk Here `git revert B..D` also works. `B..D` means `C` and `D`, and Git will revert them in the reverse sequence correctly. It's convenient when you want to revert a set of consecutive commits. Besides, the option `--no-edit` makes the process more smoothly if you don't want to edit the default revert message. – ElpieKay Apr 11 '19 at 07:49
1
git reset --hard b347
git push -f origin master

This removes c560 and d912 completely, but after that you need to force-push.

See What's the difference between Git Revert, Checkout and Reset?

To undo changes creating new commits:

git revert d912 c560
phd
  • 82,685
  • 13
  • 120
  • 165
0

Don't know what check-in is in TFVC, but if you just want to view the v2 file, you can git checkout b347 -- thefilenameyouwant

escitalopram
  • 3,750
  • 2
  • 23
  • 24
0

To continue work on the v2 state of the repository in your working copy, you can do

# get uncommitted changes out of the way
git stash

# recreate v2 in the working copy
git checkout commit-hash-or-tag-of-v2 

# create new branch based on v2
git checkout -b branchname-for-new-branch
escitalopram
  • 3,750
  • 2
  • 23
  • 24
0

(1) How to roll back and "lose" Versions 3 & 4:

It's as simple as:

git reset --hard b347

Explanation:

  • goes back to version 2.
  • you will "lose" versions 3 and 4.
  • if you still want to get versions 3 & 4 then those commits are still available in your reflog for a certain period of time depending on your settings (default time period is 90 days from memory).

or (2) Create a new branch (to keep versions 3 & 4):

  • You can simply create a new branch at version 2.

    git checkout b347

    git checkout -b redo-work-from-version-2

Explanation:

  • You'll go back to b347, in a headless state. Just ignore this for the moment.
  • We create a new branch called redo-work-from-version-2.
  • Version 3 and version 4 will still exist and are available, but they will be on a different branch.
BenKoshy
  • 33,477
  • 14
  • 111
  • 80